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

public class ContractPanel : MonoBehaviour
{
    public Contract contract;
    public TMP_Text displayName;
    public TMP_Text tonnage;
    public TMP_Text destination;
    public TMP_Text value;
    public float dismissalTime;

    public Button acceptButton;
    public Button sellButton;
    public TMP_Text transitStatus;

    private float dismissalRate;

    private bool dismissed = false;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (dismissed)
        {
            
            var rectangle = (RectTransform)transform;
            rectangle.sizeDelta = new Vector2(rectangle.sizeDelta.x, rectangle.sizeDelta.y - (Time.deltaTime * dismissalRate));
            if (rectangle.sizeDelta.y < 10)
            {
                gameObject.SetActive(false);
            }
        }
        else
        {
            displayName.text = contract.DisplayName();
            tonnage.text = contract.tonnage.ToString() + " tonnes";
            destination.text = Destination();
            value.text = contract.value.ToString() + " sterlings";
            if (contract.loaded)
            {
                if (contract.IsSellable())
                {
                    acceptButton.gameObject.SetActive(false);
                    transitStatus.gameObject.SetActive(false);
                    sellButton.gameObject.SetActive(true);
                } else
                {
                    acceptButton.gameObject.SetActive(false);
                    transitStatus.gameObject.SetActive(true);
                    transitStatus.text = "in transit";
                    sellButton.gameObject.SetActive(false);
                }
            } else
            {
                if (contract.IsLoadable())
                {
                    acceptButton.gameObject.SetActive(true);
                    transitStatus.gameObject.SetActive(false);
                    sellButton.gameObject.SetActive(false);
                } else
                {
                    acceptButton.gameObject.SetActive(false);
                    transitStatus.gameObject.SetActive(true);
                    transitStatus.text = "cannot load";
                    sellButton.gameObject.SetActive(false);
                }
            }
        }
    }

    public string Destination()
    {
        if (contract.origin == null || contract.destination == null)
        {
            return "unknown";
        } else
        {
            return contract.origin.cityName + " to " + contract.destination.cityName;
        }
    }

    public void Sell()
    {
        contract.Sell();
        Dismiss();
    }

    public void Accept()
    {
        FindObjectOfType<Airship>().Load(contract);
        GameManager.Instance().AddAirshipContract(contract);
        Dismiss();
    }

    public void Dismiss()
    {
        dismissalRate = ((RectTransform)transform).sizeDelta.y / (dismissalTime + 0.001f);
        dismissed = true;
    }
}
