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

public class QuestBehavior : MonoBehaviour
{

    public GameObject Level;
    public GameObject Inventory;
    public AudioSource AudioSuccess;
    public AudioSource AudioFail;
    public AudioSource AudioNewQuest;

    public float TimeInterval;
    public float Difficulty;
    public float DifficultyIncrease;
    public float DifficultyIncreaseIncrease;

    [HideInInspector]
    public float TimeLeft;
    [HideInInspector]
    public int InputItemID;
    [HideInInspector]
    public int InputItemCount;
    [HideInInspector]
    public int Reward;

    bool started;

    void Start()
    {
        TimeLeft = TimeInterval;   
    }


    void Update()
    {
        if (!started)
        {
            started = true;
            Shuffle(playAudio: false, firstTime: true);
        }

        TimeLeft -= Time.deltaTime;
        if (TimeLeft < 0f)
        {
            TimeLeft = 0f;
            Shuffle();
        }
        var span = TimeSpan.FromSeconds(TimeLeft);
        transform.Find("Quest Timer Text").GetComponent<TextMeshProUGUI>().text = string.Format("{0:D2}:{1:D2}", span.Minutes, span.Seconds);
        transform.Find("Quest Input Text").GetComponent<TextMeshProUGUI>().text = InputItemCount.ToString(CultureInfo.InvariantCulture);
        transform.Find("Quest Output Text").GetComponent<TextMeshProUGUI>().text = Reward.ToString(CultureInfo.InvariantCulture);
        transform.Find("Quest Input Resource").GetComponent<Image>().sprite = Level.GetComponent<Level>().ItemTextures[InputItemID];
    }

    public void Shuffle(bool playAudio = true, bool firstTime = false)
    {
        TimeLeft = TimeInterval;
        var candidates = new List<int>();
        var inventory = Inventory.GetComponent<Inventory>();
        for (var itemID = 0; itemID < Global.ITEM_COUNT; itemID++)
            if (inventory.ItemUnlocks[itemID])
                candidates.Add(itemID);
        InputItemID = candidates[Global.Rng.Next(0, candidates.Count - 1)];
        var r = Global.Rng.NextDouble();
        if (firstTime)
            InputItemCount = 10;
        else if (r <= 0.001d)
            InputItemCount = 100;
        else if (r <= 0.01d)
            InputItemCount = 50;
        else if (r <= 0.25d)
            InputItemCount = 25;
        else if (r <= 0.5d)
            InputItemCount = 10;
        else
            InputItemCount = 5;
        Reward = (int)Math.Floor(Global.ItemValue[InputItemID] * InputItemCount * Difficulty);
        Difficulty *= DifficultyIncrease;
        DifficultyIncrease *= DifficultyIncreaseIncrease;
        if (playAudio)
            AudioNewQuest.Play();
    }

    public void Deliver()
    {
        var success = TryDeliver();
        if (success)
        {
            AudioSuccess.Play();
            Shuffle();
        }
        else
            AudioFail.Play();
    }

    private bool TryDeliver()
    {
        if (TimeLeft < 0)
            return false;
        var inventory = Inventory.GetComponent<Inventory>();
        var itemCount = inventory.ItemCounts[InputItemID];
        if (itemCount < InputItemCount)
            return false;
        inventory.ItemCounts[InputItemID] -= InputItemCount;
        Level.GetComponent<Level>().Money += Reward;
        return true;
    }

}
