using System;
using System.Collections.Generic;
using System.Linq;
using eagle;
using Net.Eagle0.Eagle.Api;
using Net.Eagle0.Eagle.Common;
using Net.Eagle0.Eagle.Views;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Random = System.Random;

public class CreateGameItem : MonoBehaviour {
    public HeroDropdownController heroDropdownController;
    public TMP_Dropdown totalHumansDropdown;
    public TMP_Dropdown totalPlayersDropdown;
    public Button createButton;
    public TextMeshProUGUI buttonText;

    public Action<string, int, int> CreateCallback;

    private List<AvailableLeader> availableLeaders;

    public string SelectedLeaderTextId {
        get {
            if (heroDropdownController.SelectedHeroId == 0) {
                var index = new Random().Next(availableLeaders.Count);
                return availableLeaders[index].NameTextId;
            } else {
                return availableLeaders[heroDropdownController.SelectedHeroId - 1].NameTextId;
            }
        }
    }

    // Use this for initialization
    void Start() {}

    public void LeaderDropdownSelected(TMP_Dropdown dropdown) {}

    public void TotalPlayersDropdownSelected(TMP_Dropdown dropdown) { SetHumanPlayersDropdown(); }

    public void CreateClicked() {
        CreateCallback(
                SelectedLeaderTextId,
                totalHumansDropdown.value + 1,
                totalPlayersDropdown.value + 1);
    }

    public void SetHumanPlayersDropdown() {
        var currentValue = totalHumansDropdown.value;
        totalHumansDropdown.ClearOptions();
        totalHumansDropdown.AddOptions(Enumerable.Range(1, totalPlayersDropdown.value + 1)
                                               .Select(i => i.ToString())
                                               .ToList());
        totalHumansDropdown.value = Math.Min(totalPlayersDropdown.value, currentValue);
    }

    public void SetCreationOptions(List<AvailableLeader> leaders, int maxPlayerCount) {
        this.availableLeaders = leaders;

        buttonText.text = "CREATE";

        List<HeroView> heroOptions =
                new List<HeroView> { new() { Id = 0, Profession = Profession.NoProfession } };
        for (int i = 0; i < leaders.Count; i++) {
            var leader = leaders[i];
            heroOptions.Add(new HeroView {
                NameTextId = leader.NameTextId,
                Profession = leader.Profession,
                Id = i + 1,
                ImagePath = leader.ImagePath
            });
        }

        heroDropdownController.FallbackText = "Random";
        heroDropdownController.AvailableHeroes = heroOptions;
        heroDropdownController.HeadshotImage.gameObject.SetActive(false);

        totalPlayersDropdown.ClearOptions();
        totalPlayersDropdown.AddOptions(
                Enumerable.Range(1, maxPlayerCount).Select(i => i.ToString()).ToList());
        totalPlayersDropdown.value = Math.Min(6, maxPlayerCount - 1);

        SetHumanPlayersDropdown();
        totalHumansDropdown.value = Math.Min(totalPlayersDropdown.value, 1);
    }
}
