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

public class FlagZone : MonoBehaviour
{
    [SerializeField]
    public float TimeStayAttackers { get; private set; }
    public float TimeStayDefenders { get; private set; }
    private int occupDefenders;
    private int occupAttackers;
    private bool isOccupBoth => (occupDefenders>0) && (occupAttackers>0);
    private bool isNotOccup => (occupDefenders == 0) && (occupAttackers == 0);
    private float timeForWin;

    private void Start()
    {

        timeForWin = SettingsReader.Instance.GetSettings.timeToWin;
        Debug.Log("32");
        TimeStayAttackers = 0;
        TimeStayDefenders = 0;
        occupAttackers = 0;
        occupDefenders = 0;
    }
    private void OnTriggerEnter(Collider other)
    {
        switch(other.tag)
        {
            case "Defender":
                occupDefenders++;
                break;
            case "Attacker":
                occupAttackers++;
                break;
            default:
                Debug.LogWarning("Entered non-team entity");
                break;
        }
    }
    private void OnTriggerExit(Collider other)
    {
        switch (other.tag)
        {
            case "Defender":
                occupDefenders--;
                break;
            case "Attacker":
                occupAttackers--;
                break;
            default:
                Debug.LogWarning("Exited non-team entity");
                break;
        }
    }
    private void Update()
    {
        if (isOccupBoth || isNotOccup)
        {
            TimeStayAttackers = 0;
            TimeStayDefenders = 0;
            return;
        }
        else if (occupAttackers > 0)
        {
            TimeStayAttackers += Time.deltaTime;
            if (TimeStayAttackers > timeForWin)
                GlobalEventManager.SendCaptureFlag(Team.Attackers); 
        }
        else
        {
            TimeStayDefenders += Time.deltaTime;
            if (TimeStayDefenders > timeForWin)
                GlobalEventManager.SendCaptureFlag(Team.Defenders);
        }
    }
}
