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

public class GameManager : MonoBehaviour {
    public int BeeHits = 0;
    public int FlyHits = 0;
    public bool isLeftSideWinner = false;
    public HoneyJar hj;
    public StoreManager sm;
    public EnemyManager em;
    public PlayerManager pm;
    public AudioManager am;
    public UIManager um;
    public HoneyWave hw;
    public Spawner psa;
    public Spawner esa;
    public Vector3 StartingHoneyJarPosition;
    public Quaternion StartingHoneyJarRotation;
    public List<GoalLine> Goals;
    public bool gamePlaying = true;
    public bool showSprites = true;
    public int currentLevel = 1;
    public bool lostGame = false;

    public List<Fly> Flies;
    public List<Bee> Bees;
    public List<Clicker> BeeClickers;

    private void Start()
    {
        esa.spawnPrefab();
        psa.spawnPrefab();

        Bees[0].ForceMultiplier = 1.6f;

        um.setCurrentEnemyHitText(0);
        um.setCurrentMoneyText(0);
        um.setCurrentPlayerHitText(0);
        um.setLevelText();
        um.panel.SetActive(false);
        StopGameplay();
        um.credits.SetActive(false);
    }

    public void ProcessWinCondition()
    {
        if (isLeftSideWinner)
        {
            AdvanceLevel();
        }
        else
            DoLoseCondition();
    }

    void AdvanceLevel()
    {
        StopGameplay();

        currentLevel++;
        um.setLevelText();
        
        um.panel.SetActive(true);

        hw.DoWaveAnimation();

        sm.FlushHits();
        sm.setActiveButtons();

        if (currentLevel >= 0 && currentLevel < 5)
            em.AddUpgrade();
        if (currentLevel >= 5 && currentLevel < 10)
            em.AddUpgrade(2);
        if (currentLevel >= 10 && currentLevel < 25)
            em.AddUpgrade(3);
        if (currentLevel >= 25 && currentLevel < 50)
            em.AddUpgrade(4);
        if (currentLevel >= 50)
            em.AddUpgrade(5);

        em.actionMultiplier *= 1.15f;
    }

    void DoLoseCondition()
    {
        Debug.Log("Lose");
        gamePlaying = false;
        showSprites = false;
        lostGame = true;
        um.toggleCredits(false);
    }

    public void StartGameplay()
    {
        gamePlaying = true;
        hj.rb.gravityScale = 1;
        hj.rb.constraints = RigidbodyConstraints2D.None;
        em.spendUpgrades();
        foreach (GoalLine gl in Goals)
        {
            gl.collidedWithHoneyJar = false;
            gl.advancingLevel = false;
        }
    }

    public void StopGameplay()
    {
        gamePlaying = false;
        hj.rb.gravityScale = 0;
        hj.rb.velocity = Vector2.zero;
        hj.rb.angularVelocity = 0f;

        Vector3 newPosition = new Vector3(StartingHoneyJarPosition.x, StartingHoneyJarPosition.y + 1f, StartingHoneyJarPosition.z);
        Quaternion newRotation = Quaternion.Euler(new Vector3(0,0,6f));

        hj.transform.position = newPosition;
        hj.transform.rotation = newRotation;

        hj.rb.constraints = RigidbodyConstraints2D.FreezePositionX;
    }
}
