using UnityEngine;
using UnityEngine.InputSystem;

public class GameLogic : MonoBehaviour
{
    [SerializeField] private float MaxFallingTime = 180;

    [SerializeField] private float MinDemonClosingTime = 170;
    [SerializeField] private float MinDemonInfluanceTime = 90;

    [SerializeField] private float MaxUploadTIme = 140;

    [SerializeField] private float SaveTimeBegining = 120;
    [SerializeField] private float SaveTimeEnding = 160;

    [SerializeField] private GameObject Ship;
    [SerializeField] private GameObject DemonHead;
    [SerializeField] private GameObject Norma;
    [SerializeField] private GameObject Player;

    private float uploadStatus;
    private float heightStatus;
    private float sanityStatus;

    private bool isEscaped;
    private bool isNormaSave;
    private bool isHeightOk;
    private bool isPlayerInstane;


    void Start()
    {
        Ship.GetComponent<ShipLogic>().maxFallingTime = MaxFallingTime;
        Ship.GetComponent<ShipLogic>().minSaveEscapeTime = SaveTimeBegining;
        Ship.GetComponent<ShipLogic>().maxSaveEscapeTime = SaveTimeEnding;

        DemonHead.GetComponent<DemonHeadMovement>().minClosingTime = MinDemonClosingTime;
        DemonHead.GetComponent<DemonHeadMovement>().minInfluanceTime = MinDemonInfluanceTime;

        Norma.GetComponent<NormaLogic>().maxUploadTime = MaxUploadTIme;

    }


    private void Update()
    {

        UpdateData();

        EvaluateEnding();


    }

    void UpdateData()
    {
        isHeightOk = Ship.GetComponent<ShipLogic>().isItSaveWindow;

        isNormaSave = Norma.GetComponent<NormaLogic>().uploadCompleted;

        isPlayerInstane = Player.GetComponent<PlayerLogic>().instane;
    }


    void EvaluateEnding()
    {
        if (isEscaped)
        {
            //katapultowaliśmy się 

            if (isHeightOk)
            {
                // katapultowaliśmy się na dobrej wysokości

                if (isNormaSave)
                {
                    //1 upload ukończony
                    // GoToEnding();
                }
                else
                {
                    //2 upload nieukończony
                    // GoToEnding();
                }
            }
            else
            {
                //3 katapultowaliśmy się na złej wysokości
                // GoToEnding();
            }
        }
        else if (isPlayerInstane)
        {
            //4 Rex oszalał
            // GoToEnding();
        }
    }

    void GoToEnding()
    {
        //idź do sceny ze specjalnymi parametrami
    }



    private void OnUpload()
    {
        Norma.GetComponent<NormaLogic>().Upload();
    }

    private void OnMove(InputValue movementValue)
    {
        Player.GetComponent<PlayerMovement>().Move(movementValue);
    }

    private void OnEscape()
    {
        isEscaped = true;
    }

    private void OnSmash()
    {
        Player.GetComponent<PlayerLogic>().Smash();
    }





}
