using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine.UI;

public class Combat : MonoBehaviour {

    [SerializeField]
    GameObject enemy;

    [SerializeField]
    GameObject elementCombat;

    [SerializeField]
    GameObject player;

    [SerializeField]
    ElementGenerator eleGenerator;

    public enum Elements {Earth, Fire, Metal, Darkness, Thunder, Water };

    float timer;

    [SerializeField]
    Text timerText;

    bool timerOn;

    [SerializeField]
    Image hpBar;

    [SerializeField]
    GameObject[] hits = new GameObject[3];

    [SerializeField]
    Sprite[] enemiesSprites;

    int enemyEncounter = 0;

    [SerializeField]
    GameObject storyPlayer;

    [SerializeField]
    GameObject storyEnemy;

    public GameObject TextBar;

    [SerializeField]
    GameObject[] Dialogue;

    public bool progressing;
    float storyTimer;

    public string where;
    public GameObject[] leftRigth;

    [SerializeField]
    GameObject[] levels;

    [SerializeField]
    GameObject[] bgs;

    bool combat;

    int leftCliks;
    int rightCliks;


    void Awake()
    {
        storyTimer = 0;
        progressing = true;
        print(progressing);
        combat = false;

        rightCliks = 0;
        leftCliks = 0;
    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.H))
        {
            EndCombat();
        }

        if (Input.GetKeyDown(KeyCode.L))
        {
         //   bg.GetComponent<MeshRenderer>().material = bgs[2];
        }

        if (timerOn)
        {
            timer -= Time.deltaTime;

            timerText.text = string.Format(timer.ToString(), "00:00");

            if (timer <= 0)
            {
                GetComponent<Sotry>().GameOver();
            }

            float hp = enemy.GetComponent<Enemy>().hp / 100;

            hpBar.fillAmount = hp;
        }

        if(!progressing)
            storyTimer += Time.deltaTime;

        if(storyTimer >= 1f)
        {
            Story();
        }
    }

    public void Left()
    {
        leftRigth[0].SetActive(false);
        leftRigth[1].SetActive(false);
        leftRigth[2].SetActive(false);
        TextBar.SetActive(false);

        for (int i = 0; i < levels.Length; i++)
            levels[i].SetActive(false);

        if (leftCliks == 0)
        {
            levels[0].SetActive(true);
            leftCliks++;
        }
        else if (leftCliks == 1)
        {
            levels[1].SetActive(true);
            leftCliks++;
        }
        else if (leftCliks == 2)
        {
            levels[1].SetActive(true);
            leftCliks++;
        }
        else if (leftCliks == 3)
        {
            levels[2].SetActive(true);
            leftCliks++;
        }
        else if (leftCliks == 4)
        {
            levels[2].SetActive(true);
            leftCliks++;
        }
        else if (leftCliks == 5)
        {
            levels[3].SetActive(true);
            leftCliks++;
        }
        else if (leftCliks == 6)
        {
            levels[4].SetActive(true);
            leftCliks++;
        }

        progressing = false;
    }

    public void Prorgress()
    {
        progressing = false;
    }

    public void Right()
    {
        leftRigth[0].SetActive(false);
        leftRigth[1].SetActive(false);
        leftRigth[2].SetActive(false);
        TextBar.SetActive(false);

        for (int i = 0; i < levels.Length; i++)
        {
            levels[i].SetActive(false);
        }

        if (rightCliks == 0)
        {
            levels[1].SetActive(true);
            rightCliks++;
        }
        else if (rightCliks == 1)
        {
            levels[2].SetActive(true);
            rightCliks++;
        }
        else if (rightCliks == 2)
        {
            levels[3].SetActive(true);
            rightCliks++;
        }

        progressing = false;
        combat = true;
    }


    public void Story()
    {
        if (!combat)
        {
            progressing = true;
            TextBar.SetActive(true);
            storyTimer = 0;

            leftRigth[0].SetActive(true);
            leftRigth[1].SetActive(true);
            leftRigth[2].SetActive(true);
        }
        else
        {
            SpawnStoryEnemy();
        }
    }

    public void ChangeMap(int lvl)
    {
        for (int i = 0; i < levels.Length; i++)
        {
            levels[i].SetActive(false);
        }

        levels[lvl].SetActive(true);
    }


    public void CombatStart()
    {
        /*
        progressing = true;
        combat = false;
        storyTimer = 0;
        */
        SoundManager.Instance.PlaySound(2);

        hpBar.enabled = true;

        elementCombat.SetActive(true);
        enemy.SetActive(true);
        eleGenerator.StartGenerating();

        float timer = 1.0f;

        RandomEnemy(timer);

        foreach(GameObject obj in bgs)
        {
            obj.transform.GetChild(0).GetComponent<OffsetScroller>().move = false;
        }

        timerText.enabled = true;
        timerOn = true;
        player.SetActive(true);

        storyEnemy.SetActive(false);
        storyPlayer.GetComponent<Animator>().SetBool("Walk", true);
        storyPlayer.SetActive(false);
        
    }

    void RandomEnemy(float timeh)
    {
        int randomEnemy = enemyEncounter;

        if (randomEnemy == 0)
        {
            enemy.GetComponent<Enemy>().GenerateEnemy(100, Elements.Earth, Elements.Metal);
            enemy.GetComponent<SpriteRenderer>().sprite = enemiesSprites[0];
            timer = 60 + timeh;
        }
        else if (randomEnemy == 1)
        {
            enemy.GetComponent<Enemy>().GenerateEnemy(100, Elements.Water, Elements.Thunder);
            enemy.GetComponent<SpriteRenderer>().sprite = enemiesSprites[1];
            enemy.transform.rotation = new Quaternion(0,180,0,0);
            timer = 50f + timeh;
            timerText.enabled = true;
        }
        else if (randomEnemy == 2)
        {
            enemy.GetComponent<Enemy>().GenerateEnemy(100, Elements.Darkness, Elements.Fire);
            enemy.GetComponent<SpriteRenderer>().sprite = enemiesSprites[2];
            enemy.transform.rotation = new Quaternion(0, 0, 0, 0);
            timer = 40f + timeh;
        }

        enemyEncounter++;
    }

    public void SpawnStoryEnemy()
    {
        int randomEnemy = enemyEncounter;
        TextBar.SetActive(true);
        Dialogue[randomEnemy].SetActive(true);
        //storyEnemy.GetComponent<ParticleSystem>().Play();

        foreach (GameObject obj in bgs)
        {
            obj.transform.GetChild(0).GetComponent<OffsetScroller>().move = false;
        }

        storyEnemy.SetActive(true);
        storyPlayer.GetComponent<Animator>().SetBool("Walk", false);

        if (randomEnemy == 0)
        {
            storyEnemy.transform.rotation = new Quaternion(0, 0, 0, 0);
            storyEnemy.GetComponent<Animator>().runtimeAnimatorController = Resources.Load("Yuki") as RuntimeAnimatorController;
        }
        else if (randomEnemy == 1)
        {
            storyEnemy.transform.rotation = new Quaternion(0, 180, 0, 0);
            storyEnemy.GetComponent<Animator>().runtimeAnimatorController = Resources.Load("Kappa") as RuntimeAnimatorController;
        }
        else if (randomEnemy == 2)
        {
            storyEnemy.transform.rotation = new Quaternion(0, 0, 0, 0);
            storyEnemy.GetComponent<Animator>().runtimeAnimatorController = Resources.Load("Spider") as RuntimeAnimatorController;
        }
    }

    public void EndCombat()
    {
        if ((enemyEncounter + 1) == 1)
        {
            SoundManager.Instance.PlaySound(1);
        }
        if ((enemyEncounter + 1) == 2)
        {
            SoundManager.Instance.PlaySound(6);
        }

        elementCombat.SetActive(false);
        enemy.SetActive(false);
        eleGenerator.EndGenerating();

        foreach (GameObject obj in bgs)
        {
            obj.transform.GetChild(0).GetComponent<OffsetScroller>().move = true;
        }

        timerOn = false;
        timerText.enabled = false;
        hpBar.enabled = false;
        player.SetActive(false);
        storyPlayer.SetActive(true);
        progressing = false;

        ChangeMap(enemyEncounter + 1);
        

        GetComponent<Sotry>().ContinueProgress();
    }



    public void ElementalAttack(Elements e, int count)
    {
        int tmpInt = enemy.GetComponent<Enemy>().TakesDamage(count, e);

        int random = Random.Range(0, hits.Length);

        float x = Random.Range(8, 17);
        float y = Random.Range(-4, 10);

        Vector3 pos = new Vector3(x,y,-2f);

        GameObject tmp = Instantiate(hits[random], pos, Quaternion.identity) as GameObject;

        if (tmpInt == 1)
            tmp.GetComponent<SpriteRenderer>().color = Color.green;
        else if (tmpInt == 2)
            tmp.GetComponent<SpriteRenderer>().color = Color.red;
    }

    public Elements CheckElement(string name)
    {
        Elements value = Elements.Darkness;
        
        if (name == "1")
            value = Elements.Earth;
        else if (name == "2")
            value = Elements.Fire;
        else if (name == "3")
            value = Elements.Metal;
        else if (name == "4")
            value = Elements.Darkness;
        else if (name == "5")
            value = Elements.Thunder;
        else if (name == "6")
            value = Elements.Water;

        return value;       
    }

}
