using System;
using Net.Eagle0.Eagle.Api;
using TMPro;
using UnityEngine.UI;

namespace eagle {
    using ProvinceId = Int32;

    public class HandleRiotGiveCommandSelector : CommandSelector {
        public Toggle foodToggle;
        public Toggle goldToggle;

        public Slider foodTakenSlider;
        public TMP_Text foodTakenLabel;
        public Slider goldTakenSlider;
        public TMP_Text goldTakenLabel;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.HandleRiotGiveAvailableCommand;

        public override string HeaderString => "Give to Populace";

        public override string CommitButtonString => "Commit Gift";

        private HandleRiotGiveSelectedCommand SpecializedCommand() {
            var spc = new HandleRiotGiveSelectedCommand();
            if (foodToggle.isOn) {
                spc.FoodAmount = (int)foodTakenSlider.value;
            } else if (goldToggle.isOn) {
                spc.GoldAmount = (int)goldTakenSlider.value;
            }

            return spc;
        }

        public override SelectedCommand Command =>
                new SelectedCommand { HandleRiotGiveSelectedCommand = SpecializedCommand() };

        private HandleRiotGiveAvailableCommand HandleRiotCommand =>
                _availableCommand.HandleRiotGiveAvailableCommand;
        private ProvinceId ActingProvinceId => HandleRiotCommand.ActingProvinceId;

        protected override void SetUpUI() {
            if (HandleRiotCommand.GoldAvailable > 0) {
                goldTakenSlider.maxValue = HandleRiotCommand.GoldAvailable;
                goldTakenSlider.minValue = 1;
                goldTakenSlider.value = HandleRiotCommand.GoldAvailable;
                goldTakenLabel.text = HandleRiotCommand.GoldAvailable.ToString();
                goldToggle.enabled = true;
                goldToggle.gameObject.SetActive(true);
                foodToggle.isOn = false;
                goldToggle.isOn = true;
            } else {
                goldToggle.enabled = false;
                goldToggle.gameObject.SetActive(false);
            }

            if (HandleRiotCommand.FoodAvailable > 0) {
                foodTakenSlider.maxValue = HandleRiotCommand.FoodAvailable;
                foodTakenSlider.minValue = 1;
                foodTakenSlider.value = HandleRiotCommand.FoodAvailable;
                foodTakenLabel.text = HandleRiotCommand.FoodAvailable.ToString();
                foodToggle.enabled = true;
                foodToggle.gameObject.SetActive(true);
                goldToggle.isOn = false;
                foodToggle.isOn = true;
            } else {
                foodToggle.enabled = false;
                foodToggle.gameObject.SetActive(false);
            }
        }

        // Start is called before the first frame update
        void Start() {}

        public void FoodSliderMoved(float value) { foodTakenLabel.text = $"{(int)value}"; }

        public void GoldSliderMoved(float value) { goldTakenLabel.text = $"{(int)value}"; }

        public void FoodToggleSelected() {
            foodTakenSlider.gameObject.SetActive(true);
            foodTakenLabel.gameObject.SetActive(true);
            goldTakenSlider.gameObject.SetActive(false);
            goldTakenLabel.gameObject.SetActive(false);
        }

        public void GoldToggleSelected() {
            foodTakenSlider.gameObject.SetActive(false);
            foodTakenLabel.gameObject.SetActive(false);
            goldTakenSlider.gameObject.SetActive(true);
            goldTakenLabel.gameObject.SetActive(true);
        }

        public void CrackDownToggleSelected() {
            foodTakenSlider.gameObject.SetActive(false);
            foodTakenLabel.gameObject.SetActive(false);
            goldTakenSlider.gameObject.SetActive(false);
            goldTakenLabel.gameObject.SetActive(false);
        }

        public void NothingToggleSelected() {
            foodTakenSlider.gameObject.SetActive(false);
            foodTakenLabel.gameObject.SetActive(false);
            goldTakenSlider.gameObject.SetActive(false);
            goldTakenLabel.gameObject.SetActive(false);
        }
    }
}