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

public class CivicAI : MonoBehaviour
{

    public float boost, aiCoin;
    public float[] time = new float[13];
    public bool _;
    public float[] timeMax = new float[13];
    public bool __;
    public float waitingTime, timeImpact, timeUpgrade, timeFantom;
    public int Trigger;
    private GameObject pill;
    public GameObject[] pillObj;
    private Controller controller;

    private float chance;
    public float[] chanceUpgrade; // 0 - income, 1-6 - ability
    public float[] chanceImpact; // 0 - income, 1-6 - ability
    public float gain;



    // Start is called before the first frame update
    void Awake()
    {
        
        controller = GameObject.Find("Controller").GetComponent<Controller>();

        gain = controller.aiImpactAbility["Income"];
        time[0] = 2.5f * controller.aiCostUpAbility["Income"] / gain;
        time[1] = 2.5f * controller.aiCostUpAbility["Ability01"] / gain;
        time[2] = 2.5f * controller.aiCostUpAbility["Ability02"] / gain;
        time[3] = 2.5f * controller.aiCostUpAbility["Ability03"] / gain;
        time[4] = Random.Range(timeMax[4] / 2f,timeMax[4]);
        time[5] = 2.5f * controller.aiCostUpAbility["Ability04"] / gain;
        time[6] = Random.Range(timeMax[6] / 2f,timeMax[6]);

        time[7] = Random.Range(timeMax[7] / 2f,timeMax[7]);
        time[8] = Random.Range(timeMax[8] / 2f,timeMax[8]);
        time[9] = Random.Range(timeMax[9] / 1.15f,timeMax[9]);
        time[10] = Random.Range(timeMax[10] / 1.15f,timeMax[10]);
        time[11] = Random.Range(timeMax[11] / 1.15f,timeMax[11]);
        time[12] = 0;
    }

    void Waiting(float time)
    {
        waitingTime = time;
    }

    void AbilityUpgrade(string ability, bool status = true)
    {
        controller.aiStatusUpAbility[ability] = status;
        controller.aiStatusAbility[ability] = true;

        if (ability == "Income")
        {
            controller.ChangeValues(controller.aiImpactAbility,ability, controller.aiImpactAbility[ability] * controller.qImpact[ability],null,true);
            chanceUpgrade[0] *= 0.9f;
        }
        else
        {controller.ChangeValues(controller.aiImpactAbility,ability, controller.aiImpactAbility[ability] * controller.qImpact[ability]);}

        if (ability == "Ability02")
        {controller.ChangeValues(controller.aiCostAbility, ability, controller.aiCostAbility["Ability01"] * controller.qCost[ability]);}
        else
        {controller.ChangeValues(controller.aiCostAbility, ability, controller.aiCostAbility[ability] * controller.qCost[ability]);} 
        
        controller.ChangeValues(controller.aiCostUpAbility, ability, controller.aiCostUpAbility[ability] * controller.qCostUp[ability]);
    }       
        
    // }
    void CreatePill(string ability, bool status = true)
    {
        controller.aiStatusAbility[ability] = status;
        controller.aiStatusUpAbility[ability] = true;
        switch(ability)
        {
            case "Ability01": pill = Instantiate(pillObj[0],GameObject.Find("Civic").transform.position, Quaternion.identity); break;
            case "Ability02": pill = Instantiate(pillObj[1],GameObject.Find("Civic").transform.position, Quaternion.identity); break;
            case "Ability03": pill = Instantiate(pillObj[2],GameObject.Find("Civic").transform.position, Quaternion.identity); break;
            case "Ability04": pill = Instantiate(pillObj[3],GameObject.Find("Civic").transform.position, Quaternion.identity); break;
            case "Ability05": pill = Instantiate(pillObj[4],GameObject.Find("Civic").transform.position, Quaternion.identity); break;
            case "Ability06": pill = Instantiate(pillObj[5],GameObject.Find("Civic").transform.position, Quaternion.identity); break;
        }
        pill.GetComponent<PillMoving>().ability = ability;
        
        if (ability == "Ability02") 
        {
            pill.GetComponent<PillMoving>().impact = controller.aiImpactAbility["Ability01"];
            pill.GetComponent<PillMoving>().time = controller.aiImpactAbility[ability];
        }
        else
        {
            pill.GetComponent<PillMoving>().impact = controller.aiImpactAbility[ability];
        }
    }



    void Update()
    {
        aiCoin += controller.aiImpactAbility["Income"] * Time.deltaTime * boost;

        // 6й порядок
        if (time[12] > 0)
        {
            time[12] -= Time.deltaTime;
        }
        else
        {
            if (controller.immunity <= 10)
            {
                CreatePill("Ability06");
                time[12] = timeMax[12];
            }
        }

        //5й порядок
        if (time[11] > 0)
        {
            time[11] -= Time.deltaTime;
        }
        else
        {
            CreatePill("Ability05");
            time[11] = Random.Range(timeMax[11] / 1.15f,timeMax[11]);
        }

        //4й порядок
        if (time[10] > 0)
        {
            time[10] -= Time.deltaTime;
        }
        else
        {
            CreatePill("Ability04");
            time[10] = Random.Range(timeMax[10] / 1.15f,timeMax[10]);
        }

        //3й порядок
        if (time[9] > 0)
        {
            time[9] -= Time.deltaTime;
        }
        else
        {
            CreatePill("Ability03");
            time[9] = Random.Range(timeMax[9] / 1.15f,timeMax[9]);
        }

        //2й порядок
        if (time[8] > 0)
        {
            time[8] -= Time.deltaTime;
        }
        else
        {
            CreatePill("Ability02");
            time[8] = Random.Range(timeMax[8] / 2f,timeMax[8]);
        }

        //1й порядок
        if (time[7] > 0)
        {
            time[7] -= Time.deltaTime;
        }
        else
        {
            CreatePill("Ability01");
            time[7] = Random.Range(timeMax[7] / 2f,timeMax[7]);
        }
        
        //5й УЛУЧШЕНИЕ порядок
        if (time[5] > 0)
        {
            time[5] -= Time.deltaTime;
        }
        else
        {
            AbilityUpgrade("Ability05");
            time[5] = 2.5f * controller.aiCostUpAbility["Ability05"] / gain;
        }

        //3й УЛУЧШЕНИЕ порядок
        if (time[3] > 0)
        {
            time[3] -= Time.deltaTime;
        }
        else
        {
            AbilityUpgrade("Ability03");
            time[3] = 2.5f * controller.aiCostUpAbility["Ability03"] / gain;
        }

        //2й УЛУЧШЕНИЕ порядок
        if (time[2] > 0)
        {
            time[2] -= Time.deltaTime;
        }
        else
        {
            AbilityUpgrade("Ability02");
            time[2] = 2.5f * controller.aiCostUpAbility["Ability02"] / gain;
        }

        //1й УЛУЧШЕНИЕ порядок
        if (time[1] > 0)
        {
            time[1] -= Time.deltaTime;
        }
        else
        {
            AbilityUpgrade("Ability01");
            time[1] = 2.5f * controller.aiCostUpAbility["Ability01"] / gain;
        }

        //income УЛУЧШЕНИЕ порядок
        if (time[0] > 0)
        {
            time[0] -= Time.deltaTime;
        }
        else
        {
            AbilityUpgrade("Income");
            time[0] = 2.5f * controller.aiCostUpAbility["Income"] / gain;
        }



        if (controller.immunity <= 25)
        {
            timeMax[7] = 2f;
            timeMax[8] = 3f;
            timeMax[9] = 10f;
            timeMax[10] = 15f;
            timeMax[11] = 15f;
            timeMax[12] = 30f;
        }
        else
        {
            if (controller.immunity <= 50)
            {
                timeMax[7] = 4f;
                timeMax[8] = 6f;
                timeMax[9] = 20f;
                timeMax[10] = 30f;
                timeMax[11] = 30f;
                timeMax[12] = 40f;
            }
            else
            {
                if (controller.immunity <= 75)
                {
                    timeMax[7] = 6f;
                    timeMax[8] = 9f;
                    timeMax[9] = 30f;
                    timeMax[10] = 60f;
                    timeMax[11] = 45f;
                    timeMax[12] = 50f;
                }
                else
                {
                    timeMax[7] = 8f;
                    timeMax[8] = 12f;
                    timeMax[9] = 45f;
                    timeMax[10] = 75f;
                    timeMax[11] = 60f;
                    timeMax[12] = 60f;
                }
            }
        }
    }
}


// навсякий
/*

    void FantomPill(string ability)
    {
        if (controller.aiStatusAbility[ability] == true)
        {
            switch(ability)
            {
                case "Ability01": pill = Instantiate(pillObj[0],GameObject.Find("Civic").transform.position, Quaternion.identity); break;
                case "Ability02": pill = Instantiate(pillObj[1],GameObject.Find("Civic").transform.position, Quaternion.identity); break;
            }
            pill.GetComponent<PillMoving>().ability = ability;
            
            if (ability == "Ability02") 
            {
                pill.GetComponent<PillMoving>().impact = controller.aiImpactAbility["Ability01"];
                pill.GetComponent<PillMoving>().time = controller.aiImpactAbility[ability];
            }
            else
            {
                pill.GetComponent<PillMoving>().impact = controller.aiImpactAbility[ability];
            }
            

            timeFantom = Random.Range(4,8);
        }
    }

    void Impact()
    {
        
        chance = Random.Range(0,101);

        if (chance < 40)
        {
            CreatePill("Ability01");
        }
        else
        {
            if (chance < 70)
            {
                CreatePill("Ability02");
            }
            else
            {
                if (chance < 90)
                {
                    CreatePill("Ability03");
                }
                else
                {
                    CreatePill("Ability04", false);
                }
            }
        }
    }

        void Upgrade()
    {
        
        chance = Random.Range(0,101);
        Debug.Log("Up "+chance);
        if (upgradeCount[0] > 0)
        {
            upgradeCount[0] -= 1;
            if (upgradeCount[0] == 0) {const_upgradeCount[0] *= 1.25f;}
            AbilityUpgrade("Income");
        }
        else
        {
            if (chance <= chanceUpgradeIncome)
            {AbilityUpgrade("Income");}
            else
            {
                if (controller.time > 200)
                {AbilityUpgrade("Ability06",false);}
                else
                {
                    if (controller.time > 150)
                    {AbilityUpgrade("Ability05");}
                    else
                    {
                        if (controller.time > 100)
                        {AbilityUpgrade("Ability04",false);}
                        else
                        {
                            if (controller.time > 45)
                            {AbilityUpgrade("Ability03");}
                            else
                            {
                                if (chance < 45)
                                {AbilityUpgrade("Ability02");}
                                else
                                {AbilityUpgrade("Ability01");}
                            }
                        }
                    }
                }
            }
        }

    void Analyze()
    {
        chance = Random.Range(0,101);
        Debug.Log("Analyze "+chance);

        if (upgradeCount[0] > 0)
        {
            Upgrade();
        }
        else
        {
            if (chance <= chanceUpgrade)
            {Upgrade();}
            else
            {
                if (chance <= chanceImpact + chanceUpgrade)
                {Impact();}
                else
                { Waiting( Random.Range(timeMin, timeMax) );}
            }
        }


        
    }

    // void Upgrade()
    // {
        
    //     chance = Random.Range(0,101);
    //     Debug.Log("Up "+chance);
    //     if (upgradeCount[0] > 0)
    //     {
    //         upgradeCount[0] -= 1;
    //         if (upgradeCount[0] == 0) {const_upgradeCount[0] *= 1.25f;}
    //         AbilityUpgrade("Income");
    //     }
    //     else
    //     {
    //         if (chance <= chanceUpgradeIncome)
    //         {AbilityUpgrade("Income");}
    //         else
    //         {
    //             if (controller.time > 200)
    //             {AbilityUpgrade("Ability06",false);}
    //             else
    //             {
    //                 if (controller.time > 150)
    //                 {AbilityUpgrade("Ability05");}
    //                 else
    //                 {
    //                     if (controller.time > 100)
    //                     {AbilityUpgrade("Ability04",false);}
    //                     else
    //                     {
    //                         if (controller.time > 45)
    //                         {AbilityUpgrade("Ability03");}
    //                         else
    //                         {
    //                             if (chance < 45)
    //                             {AbilityUpgrade("Ability02");}
    //                             else
    //                             {AbilityUpgrade("Ability01");}
    //                         }
    //                     }
    //                 }
    //             }
    //         }
    //     }

*/