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

public class MenuControler : MonoBehaviour
{
    public spawner spawnerScript;
    public UiManager uiManager;
    public PlayerMovement playerMovement;
    public PLayerLife playerLife;
    public shopManager shopmanager;
    public soundManager soundmanager;
    public score Score;
    public PickupManager pickupManager;
    public GameObject playerModels;
    public bool playing;
    public GameObject menu_UI;
    public GameObject skins_UI;
    public GameObject game_UI;
    public Transform cam;
    private bool inShop;
    private float z;
    private Vector2 cameraZoomRange = new Vector2(-442.7f, -277.8f);

    [SerializeField]
    PlayGamesManager PGM;


    [SerializeField] private GameObject SigninUI;

    [SerializeField] private GameObject UserUI;


    // Start is called before the first frame update
    void Start()
    {
        spawnerScript.enabled = false;
        menu_UI.SetActive(true);
        skins_UI.SetActive(false);
        game_UI.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        if(inShop && z<1)
        {
            z += Time.deltaTime * 2;
        }else if(!inShop && z>0) 
        {
            z -= Time.deltaTime * 2;
        }
       cam.transform.localPosition = new Vector3(0f,0f,Mathf.Lerp(cameraZoomRange.x, cameraZoomRange.y, z));
    }

    public void Play()
    {
        playerLife.life = true;
        uiManager.gameOver = false;
        uiManager.gameOverAnimPlayed = false;
        shopmanager.coinsGiven = false;
        soundmanager.PlayClick1();
        spawnerScript.enabled = true;
        inShop = false;
        playing = true;
        menu_UI.SetActive(false);
        skins_UI.SetActive(false);
        game_UI.SetActive(true);
    }

    public void Skins()
    {
        soundmanager.PlayClick1();
        shopmanager.selectedSkin = shopmanager.equippedSkin;
        shopmanager.UpdateUI();
        shopmanager.UpdateButton();
        inShop = true;
        menu_UI.SetActive(false);
        skins_UI.SetActive(true);
        game_UI.SetActive(false);
    }

    public void Menu() 
    {
        if (Score.scoreAmount > Score.highscoreAmount && !uiManager.paused)
        {
            Score.highscore.SetActive(true);
            Score.highscoreAmount = Score.scoreAmount;
            PlayerPrefs.SetFloat("HighScore", Score.highscoreAmount);

            //  Save data
            PGM.OpenSave(true);

            PGM.UpdateLeaderboardScore((int)Score.scoreAmount);

            if (Score.scoreAmount >= 100)
                PGM.UnlockAchievement("warming_up");
            if(Score.scoreAmount >= 200)
                PGM.UnlockAchievement("on_fire");
            if (Score.scoreAmount >= 300)
                PGM.UnlockAchievement("untouchable");

            PGM.UpdateLeaderboardScore((int)Score.scoreAmount);
        }

        playing = false;

        soundmanager.PlayClick1();

        pickupManager.ClearPickups();
        
        uiManager.Pause(false);

        shopmanager.ShowSkin(shopmanager.equippedSkin);

        playerMovement.gameObject.transform.position = Vector3.zero;
        playerMovement.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
        playerModels.transform.rotation = Quaternion.Euler(0, 0, 0);

        playerMovement.DontAllowMovement();

        spawnerScript.enabled = false;

        uiManager.gameOver = false;

        KillAllEnemies();

        playerMovement.enabled = true;
        playerLife.Hp = 3;

        playerModels.SetActive(true);

        inShop = false;
        playing = false;

        menu_UI.SetActive(true);
        skins_UI.SetActive(false);
        game_UI.SetActive(false);
        uiManager.gameOverAnimPlayed = false;

        Score.scoreAmount = 0;
    }

    public void Quit()
    {
        //quit the game
        Application.Quit();
    }

    void KillAllEnemies()
    {
        string[] str = { "missle", "shield", "speedBoost", "flare", "misslePickup" , "parachute"};
        foreach(string s in str)
        {
            GameObject[] enemies = GameObject.FindGameObjectsWithTag(s);
            foreach (GameObject enemy in enemies)
            {
                Destroy(enemy);
            }
        }
        
    }


    public void ShowSignInUI(bool state)
    {
        if (!Application.isEditor)
        {
            SigninUI.SetActive(state);
        }
    }

    public void ShowUserUI(bool state)
    {
        if(!Application.isEditor)
        {
            UserUI.SetActive(state);
            menu_UI.SetActive(!state);
        }
    }
}
