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

public class Home : MonoBehaviour
{    
    public PlayerController OwningPlayer { get; private set; }
    public bool IsOwned { get; private set; }

    PlayerController attemptingToCapturePlayer;
    bool isCapturingPlayerOnProperty;

    [SerializeField]
    HomeOwnershipMeter meterFill;

    public bool isOwnerOnProperty;
    public int amountOfPlayersOnProperty;

    float percentToOwned = 0;

    float notOwnedAndNoCapturePlayerTick = 1;
    float notOwnedAndButCapturePlayerTick = 0.75f;
    float notBeingCapturedButOwnedTick = 0.5f;
    float ownedTickOnStay = 0.35f;


    private void Update()
    {
        if(isOwnerOnProperty == false)
        {
            if(isCapturingPlayerOnProperty == false)
            {
                if (percentToOwned > 0.00f && IsOwned == false)
                {
                    AdjustPercentToOwned(-notOwnedAndNoCapturePlayerTick);
                }
                else if (percentToOwned < 1f && IsOwned == true)
                {
                    AdjustPercentToOwned(notBeingCapturedButOwnedTick);
                }
            }

        }
        
    }




    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.GetComponent<PlayerController>())
        {
            PlayerController player = other.gameObject.GetComponent<PlayerController>();

            amountOfPlayersOnProperty++;
            if(amountOfPlayersOnProperty > GameData.ActivePlayers)
            {
                amountOfPlayersOnProperty = GameData.ActivePlayers;
            }

            if (attemptingToCapturePlayer == null || attemptingToCapturePlayer == player)
            {
                isCapturingPlayerOnProperty = true;
                ChangeAttemptingToCapturePlayer(player);
            }

            if(OwningPlayer == player)
            {
                isOwnerOnProperty = true;
            }

        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.GetComponent<PlayerController>())
        {
            if (other.gameObject.GetComponent<PlayerController>() == attemptingToCapturePlayer)
            {
                isCapturingPlayerOnProperty = false;
                attemptingToCapturePlayer = null;
            }

            if (other.gameObject.GetComponent<PlayerController>() == OwningPlayer)
            {
                isOwnerOnProperty = false;
            }

            amountOfPlayersOnProperty--;
            if (amountOfPlayersOnProperty < 0)
            {
                amountOfPlayersOnProperty = 0;
            }
        }
    }

    private void OnTriggerStay(Collider other)
    {
        if (other.gameObject.GetComponent<PlayerController>())
        {
            PlayerController player = other.gameObject.GetComponent<PlayerController>();

            //Safety
            if(OwningPlayer == attemptingToCapturePlayer)
            {
                attemptingToCapturePlayer = null;
            }

            if (IsOwned == true)
            {
                if(isOwnerOnProperty == false && percentToOwned > 0.00f)
                {
                    if (amountOfPlayersOnProperty == 1)
                    {
                        attemptingToCapturePlayer = player;
                        isCapturingPlayerOnProperty = true;
                    }

                    AdjustPercentToOwned(-ownedTickOnStay);

                    if (percentToOwned <= 0)
                    {
                        ChangeOwnership(null);
                    }
                }
                else if (isOwnerOnProperty && percentToOwned <= 1f)
                {
                    if(amountOfPlayersOnProperty == 1)
                    {
                        AdjustPercentToOwned(ownedTickOnStay);
                    }
                    
                }
            }
            else
            {
                if (amountOfPlayersOnProperty == 1)
                {
                    if(attemptingToCapturePlayer == null)
                    {
                        if(OwningPlayer != player)
                        {
                            ChangeAttemptingToCapturePlayer(player);
                            isCapturingPlayerOnProperty = true;
                        }
                        
                    }
                    else if(attemptingToCapturePlayer == player && percentToOwned < 1)
                    {
                        isCapturingPlayerOnProperty = true;

                        float adjust = IsOwned ? ownedTickOnStay : notOwnedAndButCapturePlayerTick;
                        AdjustPercentToOwned(adjust);

                        if (percentToOwned >= 1)
                        {
                            ChangeOwnership(player);
                        }
                    }
                    else if (attemptingToCapturePlayer != player && percentToOwned <= 0 && isCapturingPlayerOnProperty == false && OwningPlayer != player)
                    {
                        isCapturingPlayerOnProperty = true;
                        ChangeAttemptingToCapturePlayer(player);
                    }
                }
            }
        }
    }

    void AdjustPercentToOwned(float increment)
    {
        percentToOwned += increment * Time.deltaTime;
        meterFill.AdjustFillMeter(percentToOwned);

    }

    void ChangeOwnership(PlayerController player)
    {
        IsOwned = player != null ? true : false;
        isOwnerOnProperty = player != null ? true : false;
        percentToOwned = player != null ? 1 : 0;

        isCapturingPlayerOnProperty = false;
        attemptingToCapturePlayer = null;

        OwningPlayer = player;

        GetComponent<MeshRenderer>().material.color = player != null ? player.PlayerColor : Color.white;

        if(player != null)
        {
            GameManager.Instance.AreAllHomesOwnedByOnePlayer(player);
        }
    }

    void ChangeAttemptingToCapturePlayer(PlayerController player)
    {
        attemptingToCapturePlayer = player != null ? player : null;

        if(OwningPlayer == null)
        {
            Color color = player != null ? player.PlayerColor : Color.white;
            meterFill.ChangeMeterColorOnOwnership(color);
        }
    }


}
