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

public class Result : MonoBehaviour {

    Transform target1;
    Transform target2;
    Transform target3;
    Transform target4;
    Transform target5;
    private List<GameObject> fansA = new List<GameObject>();
    private List<GameObject> fansB = new List<GameObject>();
    private GameObject fan;
    private GameObject explosiveFan;
    private enum SceneState { ShowSideA, ShowSideB, GoingCenter, Charge, Bash, TowardToLoser, Wavality};
    private SceneState sceneState;
    private float elapsedTweenTime = 0;
    private float elapsedTime = 0;
    private int turnResult = 4;
    private int totalFansP1 = 1;
    private int totalFansP2 = 1;
    private float SwampPointA;
    private float SwampPointB;
    Vector3 startingPos;
    Vector3 endPos;
    private Animator animatorIdolP1;
    private SpriteRenderer spriteRenderIdolP1;
    private Animator animatorIdolP2;
    private SpriteRenderer spriteRenderIdolP2;
    private bool isIdolStamped = false;
    private GameObject idol1;
    private GameObject idol2;
    private GameObject p1WinSign;
    private GameObject p2WinSign;
    private GameObject fightSmoke;
    private GameObject globalObject;

    void Awake () {
        globalObject = GameObject.Find("GlobalObject");
        totalFansP1 = globalObject.GetComponent<GlobalScript>().totalP1Followers;
        totalFansP2 = globalObject.GetComponent<GlobalScript>().totalP2Followers;
        setIdolPlayer();
        p1WinSign = GameObject.Find("P1Wins");
        p2WinSign = GameObject.Find("P2Wins");
        fan = Resources.Load("FanResult") as GameObject;
        explosiveFan = Resources.Load("FanExplosive") as GameObject;
        fightSmoke = GameObject.Find("Fighting-smoke_0");
        target1 = GameObject.Find("Cube1").transform;
        target2 = GameObject.Find("Cube2").transform;
        target3 = GameObject.Find("Cube3").transform;
        target4 = GameObject.Find("Cube4").transform;
        target5 = GameObject.Find("Cube5").transform;
        createSideSquadA();
        createSideSquadB();
        sceneState = SceneState.ShowSideA;
    }

    private void setIdolPlayer()
    {
        idol1 = GameObject.Find("Idol1");
        animatorIdolP1 = idol1.transform.GetComponent<Animator>();
        spriteRenderIdolP1 = idol1.transform.GetComponent<SpriteRenderer>();
        switch (globalObject.GetComponent<GlobalScript>().player1Choice)
        {
            case 1:
                animatorIdolP1.runtimeAnimatorController = Resources.Load("Sprites/Idol1Controller") as RuntimeAnimatorController;
                spriteRenderIdolP1.sprite = Resources.Load<Sprite>("Sprites/Idol-Character-1");
                break;
            case 2:
                animatorIdolP1.runtimeAnimatorController = Resources.Load("Sprites/Idol2Controller") as RuntimeAnimatorController;
                spriteRenderIdolP1.sprite = Resources.Load<Sprite>("Sprites/Idol-Character-2");
                break;
            case 3:
                animatorIdolP1.runtimeAnimatorController = Resources.Load("Sprites/Idol3Controller") as RuntimeAnimatorController;
                spriteRenderIdolP1.sprite = Resources.Load<Sprite>("Sprites/Idol-Character-3");
                break;
            case 4:
                animatorIdolP1.runtimeAnimatorController = Resources.Load("Sprites/Idol4Controller") as RuntimeAnimatorController;
                spriteRenderIdolP1.sprite = Resources.Load<Sprite>("Sprites/Idol-Character-4");
                break;
        }
        animatorIdolP1.SetInteger("state", 2);
        idol2 = GameObject.Find("Idol2");
        animatorIdolP2 = idol2.transform.GetComponent<Animator>();
        spriteRenderIdolP2 = idol2.transform.GetComponent<SpriteRenderer>();
        switch (globalObject.GetComponent<GlobalScript>().player2Choice)
        {
            case 1:
                animatorIdolP2.runtimeAnimatorController = Resources.Load("Sprites/Idol1Controller") as RuntimeAnimatorController;
                spriteRenderIdolP2.sprite = Resources.Load<Sprite>("Sprites/Idol-Character-1");
                break;
            case 2:
                animatorIdolP2.runtimeAnimatorController = Resources.Load("Sprites/Idol2Controller") as RuntimeAnimatorController;
                spriteRenderIdolP2.sprite = Resources.Load<Sprite>("Sprites/Idol-Character-2");
                break;
            case 3:
                animatorIdolP2.runtimeAnimatorController = Resources.Load("Sprites/Idol3Controller") as RuntimeAnimatorController;
                spriteRenderIdolP2.sprite = Resources.Load<Sprite>("Sprites/Idol-Character-3");
                break;
            case 4:
                animatorIdolP2.runtimeAnimatorController = Resources.Load("Sprites/Idol4Controller") as RuntimeAnimatorController;
                spriteRenderIdolP2.sprite = Resources.Load<Sprite>("Sprites/Idol-Character-4");
                break;
        }
        animatorIdolP2.SetInteger("state", 2);
    }

    private void createSideSquadA()
    {
        for (var f = 0; f <= totalFansP1; f++)
        {
            fansA.Add(Instantiate(fan, new Vector3(Random.Range(target1.position.x - 5f, target1.position.x + 5f), Random.Range(target1.position.y - 1f, target1.position.y - 3f), -5f), Quaternion.identity) as GameObject);
        }
    }
    private void createSideSquadB()
    {
        for (var f = 0; f <= totalFansP2; f++)
        {
            fansB.Add(Instantiate(fan, new Vector3(Random.Range(target2.position.x - 5f, target2.position.x + 5f), Random.Range(target2.position.y - 1f, target2.position.y - 3f), -5f), Quaternion.identity) as GameObject);
        }
    }

    private void onExplosiveFan()
    {
        GameObject explosiveFanInit = Instantiate(explosiveFan, new Vector3(target3.position.x, target3.position.y, -5f), Quaternion.identity) as GameObject;
    }

    private void createWaveArmyA()
    {
        SwampPointA = target4.position.x - 15f;
        float startingPoint = SwampPointA;
        for (var f = 0; f <= 29; f++)
        {
            fansA.Add(Instantiate(fan, new Vector3(startingPoint, Random.Range(target1.position.y - 1f, target1.position.y - 3f), -5f), Quaternion.identity) as GameObject);
            startingPoint = startingPoint - 1f;
        }
    }
    private void createWaveArmyB()
    {
        SwampPointB = target5.position.x + 15f;
        float startingPoint = SwampPointB;
        for (var f = 0; f <= 29; f++)
        {
            fansB.Add(Instantiate(fan, new Vector3(startingPoint, Random.Range(target2.position.y - 1f, target2.position.y - 3f), -5f), Quaternion.identity) as GameObject);
            startingPoint = startingPoint + 1f;
        }
    }

    private void FixedUpdate()
    {
        switch (sceneState)
        {
            case SceneState.ShowSideA:
                    elapsedTime = 0;
                    elapsedTweenTime = 0;
                    startingPos = new Vector3(target1.position.x, target1.position.y, -38.3f);
                    endPos = new Vector3(target2.position.x, target2.position.y, -38.3f);
                    sceneState = SceneState.ShowSideB;   
                break;
            case SceneState.ShowSideB:
                if (elapsedTime < 1.5f)
                {
                    elapsedTime += Time.deltaTime;
                }
                else
                {
                    if (elapsedTweenTime < 2f)
                    {
                        transform.position = Vector3.Lerp(startingPos, endPos, (elapsedTweenTime / 2f));
                        elapsedTweenTime += Time.deltaTime;
                    }
                    else
                    {
                        elapsedTime = 0;
                        startingPos = new Vector3(target2.position.x, target2.position.y, -38.3f);
                        endPos = new Vector3(target3.position.x, target3.position.y, -38.3f);
                        elapsedTweenTime = 0;
                        sceneState = SceneState.GoingCenter;
                    }
                }
                break;
            case SceneState.GoingCenter:
                if (elapsedTime < 1.5f)
                {
                    elapsedTime += Time.deltaTime;
                }
                else
                {
                    if (elapsedTweenTime < 2f)
                    {
                        transform.position = Vector3.Lerp(startingPos, endPos, (elapsedTweenTime / 2f));
                        elapsedTweenTime += Time.deltaTime;
                    }
                    else
                    {
                        sceneState = SceneState.Charge;
                    }
                }
                break;
            case SceneState.Charge:
                foreach (GameObject fanA in fansA)
                {
                    if (fanA != null)
                    {
                        fanA.transform.position = new Vector3(fanA.transform.position.x + 1f, fanA.transform.position.y, fanA.transform.position.z);
                        if (fanA.transform.position.x >= target3.position.x)
                        {
                            fansA.Remove(fanA); Destroy(fanA);
                            fightSmoke.transform.position = new Vector3(fightSmoke.transform.position.x, fightSmoke.transform.position.y, -3.7f);
                        }
                    }
                }
                foreach (GameObject fanB in fansB)
                {
                    if (fanB != null)
                    {
                        fanB.transform.position = new Vector3(fanB.transform.position.x - 1f, fanB.transform.position.y, fanB.transform.position.z);
                        if (fanB.transform.position.x <= target3.position.x)
                        {
                            fansB.Remove(fanB); Destroy(fanB);
                            fightSmoke.transform.position = new Vector3(fightSmoke.transform.position.x, fightSmoke.transform.position.y, -3.7f);
                        }
                    }
                }
                if(fansA.Count == 0 && fansB.Count == 0)
                {
                    elapsedTime = 0;
                    sceneState = SceneState.Bash;
                }
                break;
            case SceneState.Bash:
                if(elapsedTime < 2f)
                {
                    elapsedTime += Time.deltaTime;
                } else
                {
                    turnResult--;
                    if (turnResult > 0)
                    {
                        elapsedTime = 0;
                        onExplosiveFan();
                    } else
                    {
                        if (totalFansP1 >= totalFansP2)
                        {
                            startingPos = new Vector3(target3.position.x, target3.position.y, -38.3f);
                            endPos = new Vector3(target4.position.x, target4.position.y, -38.3f);
                        }
                        else
                        {
                            startingPos = new Vector3(target3.position.x, target3.position.y, -38.3f);
                            endPos = new Vector3(target5.position.x, target5.position.y, -38.3f);

                        }
                        elapsedTweenTime = 0;
                        sceneState = SceneState.TowardToLoser;
                    }
                }
                break;
            case SceneState.TowardToLoser:
                if (elapsedTweenTime < 2f)
                {
                    transform.position = Vector3.Lerp(startingPos, endPos, (elapsedTweenTime / 2f));
                    elapsedTweenTime += Time.deltaTime;
                }
                else
                {
                    if (totalFansP1 >= totalFansP2)
                    {
                        createWaveArmyA();
                        startingPos = new Vector3(p1WinSign.transform.position.x, p1WinSign.transform.position.y, p1WinSign.transform.position.z);
                        endPos = new Vector3(p1WinSign.transform.position.x, 0.5f, -5.39f);
                    } else
                    {
                        createWaveArmyB();
                        startingPos = new Vector3(p2WinSign.transform.position.x, p2WinSign.transform.position.y, p2WinSign.transform.position.z);
                        endPos = new Vector3(p2WinSign.transform.position.x, 0.5f, -5.39f);
                    }
                    elapsedTweenTime = 0;
                    elapsedTime = 0;
                    sceneState = SceneState.Wavality;
                }
                break;
            case SceneState.Wavality:
                if (fansA.Count > 0)
                {
                    foreach (GameObject fanA in fansA)
                    {
                        if (fanA != null)
                        {
                            fanA.transform.GetComponent<SpriteRenderer>().flipX = true;
                            fanA.transform.position = new Vector3(fanA.transform.position.x + 0.25f, fanA.transform.position.y, fanA.transform.position.z);
                            if (fanA.transform.position.x>= target4.position.x + 15f)
                            {
                                
                                fanA.transform.position = new Vector3(SwampPointA, fanA.transform.position.y, fanA.transform.position.z);  
                            }
                            if(fanA.transform.position.x >= target4.position.x)
                            {
                                isIdolStamped = true;
                            }
                        }
                    }
                    if(isIdolStamped)
                    {
                        idol2.transform.position = new Vector3(idol2.transform.position.x + 0.20f, idol2.transform.position.y, idol2.transform.position.z);
                            if (elapsedTweenTime < 3f)
                            {
                                p1WinSign.transform.position = Vector3.Lerp(startingPos, endPos, (elapsedTweenTime / 0.5f));
                                elapsedTweenTime += Time.deltaTime;
                            }
                        else
                        {
                            if (elapsedTime <= 3f)
                            {
                                elapsedTime++;
                            }
                            else
                            {
                                SceneManager.LoadSceneAsync("Title");
                            }

                        }
                    }
                }
                if (fansB.Count > 0)
                {
                    foreach (GameObject fanB in fansB)
                    {
                        if (fanB != null)
                        {
                            fanB.transform.position = new Vector3(fanB.transform.position.x - 0.25f, fanB.transform.position.y, fanB.transform.position.z);
                            if (fanB.transform.position.x <= target5.position.x - 15f)
                            {
                                fanB.transform.position = new Vector3(SwampPointB, fanB.transform.position.y, fanB.transform.position.z);
                            }
                            if (fanB.transform.position.x <= target5.position.x)
                            {
                                isIdolStamped = true;
                            }
                        }
                    }
                    if (isIdolStamped)
                    {
                        idol1.transform.position = new Vector3(idol2.transform.position.x - 0.20f, idol2.transform.position.y, idol2.transform.position.z);

                            if (elapsedTweenTime < 3f)
                            {
                                p2WinSign.transform.position = Vector3.Lerp(startingPos, endPos, (elapsedTweenTime / 0.5f));
                                elapsedTweenTime += Time.deltaTime;
                            } else{
                                 if(elapsedTime <= 3f)
                                {
                                    elapsedTime++;
                                 } else
                                 {
                                    SceneManager.LoadSceneAsync("Title");
                                }
                                 
                            }
                    }
                }
                break;
        }
    }
}
