using System;
using UnityEngine;

public class Building : MonoBehaviour
{
    public BuildingData buildingData;
    public int curLevel;
    public Outline[] outlines;
    public bool autoGetOutlines = true;
     
    public DateTime lastCollected;

    void OnDrawGizmos() {
        Gizmos.color = Color.blue;
        //Gizmos.DrawWireSphere(transform.position + new Vector3(0,spaceRadius,0), spaceRadius);
        Collider col = GetComponent<Collider>();
        Gizmos.DrawWireCube(col.bounds.center, col.bounds.size);
    }

    void Awake(){
        //if(outline == null){outline = GetComponent<Outline>();}
       if(autoGetOutlines){ outlines = GetComponentsInChildren<Outline>(); }
        ToggleOutlines(false);
        Selector.OnSelectedChanged.AddListener(OnSelectedChanged);
        transform.tag = "Building";
    }

    void OnSelectedChanged(){
        if(Selector.selectedBuilding == null){ToggleOutlines(false); return;}
        
        ToggleOutlines(Selector.selectedBuilding == this);
        //GetComponent<Collider>().isTrigger=(Selector.movingBuilding==this);

    }

    void ToggleOutlines(bool value){
        bool isMoving = Selector.movingBuilding==this;
        foreach(Outline outline in outlines){
            outline.enabled = value;
            outline.OutlineColor = (isMoving) ? Color.green : Color.yellow;
        }
    }

    public void ChangeOutlineColor(Color color){
        foreach(Outline outline in outlines){
            outline.OutlineColor = color;
        }
    }

    public void Upgrade(){
        if(curLevel >= buildingData.levels.Count-1){Debug.Log("Already max");return;}
        int cost = buildingData.levels[curLevel+1].price;
        // Debug.Log("Upgrading " + buildingData.buildingName + " for " + cost + " coins");

        switch (curLevel)
            {
                case 0: Debug.Log("1 Star"); break;
                case 1: Debug.Log("2 Stars"); break;
            }


        DBmanager.SetCoins(DBmanager.Coins-cost);
        Mathf.Clamp(curLevel++,0, buildingData.levels.Count-1);

        DBmanager.UpgradeBuilding(buildingData.buildingName, curLevel);
        DBmanager.SetXp(DBmanager.Xp + buildingData.levels[curLevel].xpGain);
    }
//
//    public List<Collider> buildingsInsideMe = new List<Collider>();
//    public bool locationInvalid {get{return buildingsInsideMe.Count == 0;}}
//
//    void OnTriggerEnter(Collider other) {
//        Debug.Log("Trigger enter : " + other.name);
//        if(other.GetComponent<Collider>().tag == "Building" && other != GetComponent<Collider>()){
//            if(buildingsInsideMe.Contains(other)){
//                //Already got him
//            }else{
//                buildingsInsideMe.Add(other);ChangeOutlineColor(locationInvalid ? Color.red : Color.green);
//            }
//        }
//
//
//    }
//
//    void OnTriggerExit(Collider other){
//        Debug.Log("Trigger exit : " + other.name);
//        if(other.GetComponent<Collider>().tag == "Building" && other != GetComponent<Collider>()){
//            if(buildingsInsideMe.Contains(other)){
//                buildingsInsideMe.Remove(other);
//                ChangeOutlineColor(locationInvalid ? Color.green : Color.red);
//            }else{
//                //Nothing to remove
//            }
//        }
//
//    }


    /*  */
}


[System.Serializable]
public class BuildingState{
    public string id;
    public int level;
    public Vector3 position;
    public DateTime lastCollectedTimestamp;

    public BuildingState(string m_id, int m_level, Vector3 m_position, DateTime _lastCollectedTimestamp){
        id = m_id;
        level = m_level;
        position = m_position;
        lastCollectedTimestamp= _lastCollectedTimestamp;
    }
}