using System.Collections.Generic;
using System.Linq;
using common;
using Net.Eagle0.Eagle.Views;
using UnityEngine;

namespace eagle {
    public class FreeHeroesTableController : MonoBehaviour {
        public GameObject panel;

        private ProvinceView _currentProvince;
        public ProvinceView CurrentProvince {
            get => _currentProvince;
            set {
                if (_currentProvince != value) {
                    _currentProvince = value;
                    UpdateTables();
                }
            }
        }

        private IGameModel _model;
        public IGameModel Model { private
            get => _model;
            set {
                _model = value;
                UpdateTables();
                panel.SetActive(value != null);
            }
        }

        public EventBasedTable unaffiliatedHeroesTable;

        public CommandPanelController commandPanelController;

        private CommandSelector CommandSelector =>
                commandPanelController == null ? null : commandPanelController.Selector;

        public void UpdateTables() {
            if (Model == null) {
                panel.SetActive(false);
                return;
            }
            panel.SetActive(true);
            setUpUnaffiliatedHeroesTable();
        }

        private void SetUnaffiliatedHeroRowSelections(
                UnaffiliatedHeroRowController row,
                UnaffiliatedHeroBasics uh) {
            if (CommandSelector == null) {
                row.Selected = false;
                row.Selectable = false;
                row.RightSelectable = false;
            } else {
                row.Selected = commandPanelController.Selector.TargetedHeroIds.Contains(uh.HeroId);
                row.Selectable = commandPanelController.Selector.HeroIsTargetable(uh.HeroId);
                row.RightSelectable = false;
            }
        }

        private void UpdateUnaffiliatedHeroSelections() {
            UnaffiliatedHeroes.Each(
                    (uh, i) => SetUnaffiliatedHeroRowSelections(
                            unaffiliatedHeroesTable.ComponentAt<UnaffiliatedHeroRowController>(i),
                            uh));
        }

        public void UnaffiliatedHeroRowClicked(int index) {
            var heroId = UnaffiliatedHeroes.ElementAt(index).HeroId;
            if (CommandSelector != null) { CommandSelector.AddTargetedHero(heroId); }
            UpdateUnaffiliatedHeroSelections();
        }

        private void setUpUnaffiliatedHeroesTable() {
            unaffiliatedHeroesTable.RowCount = UnaffiliatedHeroCount;

            UnaffiliatedHeroes.Each((uh, i) => {
                var newRow = unaffiliatedHeroesTable.ComponentAt<UnaffiliatedHeroRowController>(i);
                newRow.SetUnaffiliatedHero(uh, Model);
                SetUnaffiliatedHeroRowSelections(newRow, uh);
            });
        }

        void Start() { panel.SetActive(Model != null); }

        private bool HasProvinceInfo =>
                (CurrentProvince != null && CurrentProvince.FullInfo != null);

        private int UnaffiliatedHeroCount =>
                HasProvinceInfo ? CurrentProvince.FullInfo.UnaffiliatedHeroes.Count : 0;
        private List<UnaffiliatedHeroBasics> UnaffiliatedHeroes =>
                HasProvinceInfo ? CurrentProvince.FullInfo.UnaffiliatedHeroes
                                          .OrderByDescending(
                                                  uh => Model.Heroes[uh.HeroId],
                                                  HeroComparer.Shared)
                                          .ToList()
                                : new List<UnaffiliatedHeroBasics>();
    }
}