using System;
using System.Linq;
using common;
using Net.Eagle0.Common;
using Net.Eagle0.Eagle.Api;
using Net.Eagle0.Eagle.Api.Command.Util;
using Net.Eagle0.Eagle.Views;
using UnityEngine;
using UnityEngine.UI;

namespace eagle {
    using ProvinceId = Int32;

    public class FreeForAllDecisionCommandSelector : CommandSelector {
        public EventBasedTable unitsTable;
        public EventBasedTable armiesTable;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.FreeForAllDecisionCommand;
        public override string HeaderString => "Free-For-All Decision";
        public override string CommitButtonString =>
                AdvanceToggle.isOn ? "Commit Advance" : "Commit Withdraw";

        public Toggle AdvanceToggle;
        public Toggle WithdrawToggle;

        private FreeForAllDecisionAvailableCommand FreeForAllDecisionCommand =>
                _availableCommand.FreeForAllDecisionCommand;
        private ProvinceId ActingProvinceId => FreeForAllDecisionCommand.ProvinceId;

        public override SelectedCommand Command => new SelectedCommand {
            FreeForAllDecisionCommand =
                    new FreeForAllDecisionSelectedCommand {
                        ProvinceId = ActingProvinceId,
                        Decision =
                                AdvanceToggle.isOn
                                        ? new AttackDecisionType { Advance = new AdvanceDecision() }
                                        : new AttackDecisionType { Withdraw = new WithdrawDecision() }
                    }
        };

        protected override void SetUpUI() {
            unitsTable.RowCount = FreeForAllDecisionCommand.ActingUnits.Count;
            ProvinceView province = _model.Provinces[ActingProvinceId];

            FreeForAllDecisionCommand.ActingUnits.Each((cu, idx) => {
                CombatUnitRowController row = unitsTable.ComponentAt<CombatUnitRowController>(idx);
                row.Hero = cu.Hero;
                row.Battalion = cu.Battalion;
            });

            armiesTable.RowCount = FreeForAllDecisionCommand.Armies.Count;
            FreeForAllDecisionCommand.Armies.Each((army, idx) => {
                IncomingArmyTableRow row = armiesTable.ComponentAt<IncomingArmyTableRow>(idx);
                row.Faction = _model.FactionName(army.FactionId);
                row.HeroCount = army.HeroCount;
                row.TroopCount = army.TroopCount;
                switch (army.Hostility) {
                    case Hostility.AlliedHostility: row.TextColor = Color.green; break;
                    case Hostility.SelfHostility: row.TextColor = Color.blue; break;
                    case Hostility.EnemyHostility: row.TextColor = Color.red; break;
                }
                row.Origin = _model.Provinces[army.OriginProvinceId].Name;
            });

            AdvanceToggle.isOn = true;
            AdvanceToggle.enabled = true;
            WithdrawToggle.enabled = FreeForAllDecisionCommand.AvailableDecisions.Any(
                    x => x.SealedValueCase == AttackDecisionType.SealedValueOneofCase.Withdraw);
        }

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