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

namespace eagle {
    using ProvinceId = Int32;

    public class TradeCommandSelector : CommandSelector {
        public TMP_Text buyPrice;
        public TMP_Text sellPrice;
        public Slider slider;
        public TMP_Text goldText;
        public TMP_Text foodText;
        public TMP_Text description;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.TradeCommand;
        public override string HeaderString => "Trade";
        public override string CommitButtonString => IsBuy ? "Commit Buy" : "Commit Sell";

        private TradeAvailableCommand TradeCommand => _availableCommand.TradeCommand;
        private ProvinceId ActingProvinceId => TradeCommand.ActingProvinceId;

        private int MaxFood =>
                TradeCommand.FoodAvailable +
                (int)Math.Floor(TradeCommand.GoldAvailable / TradeCommand.FoodBuyPrice);
        private int FoodAmount => (int)slider.value;

        protected override void SetUpUI() {
            slider.minValue = 0;
            slider.maxValue = MaxFood;
            slider.value = TradeCommand.FoodAvailable;

            buyPrice.text = $"{TradeCommand.FoodBuyPrice:0.##}";
            sellPrice.text = $"{TradeCommand.FoodSellPrice:0.##}";

            SetTextFields();
        }

        public void GoldPlusClicked() {
            slider.value = Math.Max(0, slider.value - KeyModifiedAmount.Amount());
            SetTextFields();
        }

        public void FoodPlusClicked() {
            slider.value = Math.Min(MaxFood, slider.value + KeyModifiedAmount.Amount());
            SetTextFields();
        }

        public override bool EnableCommitButton => slider.value != TradeCommand.FoodAvailable;
        public override string DisabledCommitButtonReason => "No change";

        private bool IsBuy => FoodAmount > TradeCommand.FoodAvailable;
        private int NetFood => FoodAmount - TradeCommand.FoodAvailable;
        private int NetGold => IsBuy ? -(int)Math.Ceiling(NetFood * TradeCommand.FoodBuyPrice)
                                     : -(int)Math.Ceiling(NetFood * TradeCommand.FoodSellPrice);
        private int GoldAmount => TradeCommand.GoldAvailable + NetGold;

        public override SelectedCommand Command => new() {
            TradeCommand =
                    new TradeSelectedCommand {
                        Amount = Math.Abs(FoodAmount - TradeCommand.FoodAvailable),
                        TradeType = IsBuy ? TradeSelectedCommand.Types.TradeType.BuyFood
                                          : TradeSelectedCommand.Types.TradeType.SellFood
                    }
        };

        public void SetTextFields() {
            goldText.text = GoldAmount.ToString();
            foodText.text = FoodAmount.ToString();

            description.text = IsBuy ? $"Buying {NetFood} food for {-NetGold} gold"
                                     : $"Selling {-NetFood} food for {NetGold} gold";
        }
    }
}