using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;

/*public class Target
{
    public FoodType Type;
    public int Number;

    public Target(FoodType t, int n)
    {
        Type = t;
        Number = n;
    }
}*/

public class GameController_version1 : MonoBehaviour
{/*
    //a class for keeping scores, trigger game over, initiate objects, etc.
    public static GameController Instance;

    public Rect SpawnArea;
    public List<Transform> WanderPoints;
    public Player PlayerController;
    public Image Img;
    public Text Number;
    public List<LevelConfig> LevelConfigList;
    public List<FoodAsset> FoodAssetList;

    //scorekeeping
    public int current_score;

    private LevelConfig currentLevelConfig;
    public FoodType TargetFoodType
    {
        get { return targetList[0].Type; }
    }

    public int TargetFoodNumber
    {
        get { return targetList[0].Number; }
    }

    private int currentLevel = 0;
    private List<float> timeCountList = new List<float>();
    private Dictionary<FoodType, int> foodMap = new Dictionary<FoodType, int>();
    private List<Target> targetList = new List<Target>();

    void Awake()
    {
        Instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        foodMap.Clear();
        timeCountList.Clear();

        currentLevelConfig = LevelConfigList[currentLevel];
        var target = new Target(currentLevelConfig.TargetFoodType, currentLevelConfig.TargetFoodNumber);
        targetList.Add(target);

        var asset = FoodAssetList.Find(x => x.Type == TargetFoodType);
        Img.sprite = asset.Sp;
        Number.text = $"x{currentLevelConfig.TargetFoodNumber}";
        foreach (var ele in currentLevelConfig.FoodList)
        {
            timeCountList.Add(Random.Range(ele.RefreshIntervalLower, ele.RefreshIntervalUpper));
            foodMap[ele.Type] = 0;
        }

        //initial score
        //testing; should be 0
        current_score = 15;
    }

    // Update is called once per frame
    void Update()
    {
        if (targetList.Count == 0)
        {
            SceneManager.LoadScene("Success_lv1");
            Debug.Log("==========================>½áÊøÁË");
        }
        else
        {
            if (TargetFoodNumber == 0)
            {
                targetList.RemoveAt(0);
                var asset = FoodAssetList.Find(x => x.Type == TargetFoodType);
                Img.sprite = asset.Sp;
                Number.text = $"x{TargetFoodNumber}";
            }
        }

        for (int i = 0; i < currentLevelConfig.FoodList.Count; i++)
        {
            var levelFoodConfig = currentLevelConfig.FoodList[i];
            timeCountList[i] = timeCountList[i] - Time.deltaTime;
            if (timeCountList[i] < 0)
            {
                timeCountList[i] = Random.Range(levelFoodConfig.RefreshIntervalLower, levelFoodConfig.RefreshIntervalUpper);
                if (foodMap[levelFoodConfig.Type] < levelFoodConfig.MaxNumber)
                {
                    int num = levelFoodConfig.MaxNumber - foodMap[levelFoodConfig.Type];
                    num = Mathf.Min(Random.Range(levelFoodConfig.RefreshNumLower, levelFoodConfig.RefreshNumUpper), num);
                    for (int ii = 0; ii < num; ii++)
                    {
                        var asset = FoodAssetList.Find(x => x.Type == levelFoodConfig.Type);
                        var go = Instantiate(asset.Prefab);
                        go.transform.position = new Vector3(SpawnArea.x + Random.Range(0, SpawnArea.width), SpawnArea.y + Random.Range(0, SpawnArea.height), 0);
                        foodMap[levelFoodConfig.Type]++;
                    }
                }
            }
        }
    }

    public void DestroyFood(FoodType type)
    {
        if (foodMap.ContainsKey(type))
        {
            foodMap[type]--;
        }
    }

    public void EatFood(FoodType type)
    {
        if (foodMap.ContainsKey(type))
        {
            foodMap[type]--;
        }

        //I see the type of golden teech is now teeth but golden teeth is also an option for type
        //Q: will this work if type could be either teeth or golden teeth?
        if (type == TargetFoodType)
        {
            Scored();
            targetList[0].Number--;
            Number.text = $"x{targetList[0].Number}";
        }
        else
        {
            LostPoint();
        }
    }

    public void Scored()
    {
        current_score++;
        if (current_score >= 30)
            SceneManager.LoadScene("Success_lv1");

    }

    public void LostPoint()
    {
        //current_score--;
        if (current_score <= 0)
        {
            SceneManager.LoadScene("Fail_lv1");
        }

    }

    public Vector3 GetRandomWanderPoint()
    {
        int index = Random.Range(0, WanderPoints.Count);
        return WanderPoints[index].position;
    }*/
}
