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

public class GameManager : MonoBehaviour
{
    static GameManager _instance;

    public Airship airship;
    public List<City> cities;
    public CargoDetailsPanel cargoDetailsPanel;
    public ContractManager contractManager;
    public BrokeragePanel brokeragePanel;
    public TimeManager timeManager;

    public static GameManager Instance()
    {
        return _instance;
    }

    private void Awake()
    {
        _instance = this;
        contractManager = FindObjectOfType<ContractManager>();
    }

    // Start is called before the first frame update
    void Start()
    {
        cargoDetailsPanel.ClearContracts();

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void AssignAirship(City city)
    {
        airship.SetLocation(city);
        brokeragePanel.airship = airship;

        // TODO: don't auto assign contracts
        foreach (var contract in airship.contracts)
        {
            AddAirshipContract(contract);
            contract.destination = airship.location;
            if (contract != airship.contracts[0])
            {
                contract.destination = cities[4];
            }
            contract.origin = cities[(int)Random.Range(2, 12)];
        }

        for (int i = contractManager.MaxContracts(airship.location); i > 0; i--)
        {
            AddBrokerageContract(contractManager.BuildContract(airship.location));
        }

        // airship.location = null;
    }

    public void AssignCities(List<City> newCities)
    {
        cities = newCities;
        AssignAirship(cities[0]);
    }

    public void AddAirshipContract(Contract contract)
    {
        cargoDetailsPanel.AddContract(contract);
    }

    public void AddBrokerageContract(Contract contract)
    {
        brokeragePanel.AddContract(contract);
    }

    public bool Paused()
    {
        return timeManager.Paused() || (airship.location != null);
    }

    public void NextDay()
    {
        airship.DeductExpenses();
    }

    public void AdvanceTime(float hours)
    {
        airship.ConsumeFuel(hours);
    }

    public void ActivateDocking()
    {
        brokeragePanel.ClearContracts();
        for (int i = contractManager.MaxContracts(airship.location); i > 0; i--)
        {
            AddBrokerageContract(contractManager.BuildContract(airship.location));
        }
    }
}
