using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveKangaroos : MonoBehaviour
{
    public RectTransform posKangaroo;
    public UIBeginEndZone zones;
    public Rigidbody2D body;
    public RectTransform parent;
    public RectTransform greatgranparent;
    public Vector2 speed;
    public bool direction = true;
    public ResourceType resource;
    public int resourceCounter;
    public RecollectionSectionManager gameManager;
    Vector2 realEndZone;
    Vector2 realBeginZone;
    bool fix = false;

    // Start is called before the first frame update
    void Start()
    {
        body.velocity = speed;
    }

    public void startLaneMovement() {
        body.velocity = speed;
        body.constraints &= ~RigidbodyConstraints2D.FreezePositionX;
    }

    // Update is called once per frame
    void Update()
    {
        //posKangaroo.sizeDelta = new Vector2(0.26711f * greatgranparent.sizeDelta.y + 26.1f, posKangaroo.sizeDelta.y);
        if (!fix)
        {
            zones.beginZone = new Vector2(0.26711f * greatgranparent.sizeDelta.y + 26.1f, 0);
            fix = true;
        };
        realEndZone = new Vector2(zones.endZone.x, posKangaroo.anchoredPosition.y);
        realBeginZone = new Vector2(zones.beginZone.x, posKangaroo.anchoredPosition.y);
        if (posKangaroo.anchoredPosition.x >= realEndZone.x && direction != false)
        {
            direction = false;
            float random = Random.Range(0.0f, 1.0f);
            int mask = 1;
            if (random < 0.01f) {
                mask = -1;
            }
            posKangaroo.localScale = new Vector3(-1*mask,1,1);
            body.velocity = body.velocity * -1;
        }
        if (posKangaroo.anchoredPosition.x <= realBeginZone.x && direction != true) {
            direction = true;
            float random = Random.Range(0.0f, 1.0f);
            int mask = 1;
            if (random < 0.01f)
            {
                mask = -1;
            }
            posKangaroo.localScale = new Vector3(1*mask, 1, 1);
            body.velocity = body.velocity * -1;
            resourceCounter += 1;
            gameManager.IncreaseResourceCounter(1, (int)resource);
        }
    }
}
