using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;

public class House : MonoBehaviour
{
    public static House instance;
    public List<Furniture> furnishings;

    public TextMeshProUGUI txtAngle;

    private void Awake()
    {
        instance = this;
        GameState.instance.CloseToLoss = false;
    }

    private void Update()
    {
        if (GameState.instance.isEndGame)
        {
            return;
        }

        //Get the total moment
        float moment = furnishings.Sum(f => f.Moment);

        moment += GameState.instance.finalLevelMoment.Sum(l => l * GameSettings.settings.previousLevelMultiplier);

        float rotate = - 1 * moment * GameSettings.settings.rotationSpeed * Time.deltaTime;

        var previousRotation = this.transform.rotation;

        var eulerRot = this.transform.rotation.eulerAngles;

        if ((eulerRot.z > 0 && eulerRot.z < 45))
        {
            //We're on the left - so are we becoming balancy or unbalanced?
            if (rotate < 0)
            {
                //Balancing
                rotate = -1 * moment * GameSettings.settings.rotationSpeed * Time.deltaTime;
            }
            else
            {
                //Disbalancing
                rotate = -1 * moment * GameSettings.settings.disbalanceSpeed * Time.deltaTime;
            }
        }
        else
        {

            //We're on the right - so are we becoming balancy or unbalanced?
            if (rotate > 0)
            {
                //Balancing
                rotate = -1 * moment * GameSettings.settings.rotationSpeed * Time.deltaTime;
            }
            else
            {
                //Disbalancing
                rotate = -1 * moment * GameSettings.settings.disbalanceSpeed * Time.deltaTime;
            }
        }


        //Now we need to translate moment to rotation
            this.transform.Rotate(0, 0, rotate);
        
        eulerRot = this.transform.rotation.eulerAngles;

        if (txtAngle)
        {
            txtAngle.text = eulerRot.z.ToString("0");
        }

        if ( (eulerRot.z > 30 && eulerRot.z < 45) || (eulerRot.z < 330 && eulerRot.z > 315))
        {
            //Close to death
            GameState.instance.CloseToLoss = true;
        }
        else
        {
            GameState.instance.CloseToLoss = false;
        }

        if ( eulerRot.z < 45 || eulerRot.z > 315)
        {
            //Okay
        }
        else
        {
            this.transform.rotation = previousRotation;

            //You lose!
            UIManager.instance.GameOver();
            this.gameObject.AddComponent<Rigidbody2D>();
            this.gameObject.GetComponent<Rigidbody2D>().gravityScale = 1;
            //Destroy(this.transform.Find("Line").gameObject); //Remove after testing
            GameState.instance.isEndGame = true;
            Time.timeScale = 0.75f;

            SoundManager.instance.Lose();
        }
    }
}
