using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Threading.Tasks;
using System;
using WpfAppA.model;
using System.IO;
using System.Runtime.Serialization.Json;
using WpfAppA.Windows;

namespace WpfAppA
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window, INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        private void Invalidate()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("HeroList"));
            {
                InitializeComponent();

                Style buttonStyle = new Style();
                buttonStyle.Setters.Add(
                    new Setter
                    {
                        Property = Control.FontFamilyProperty,
                        Value = new FontFamily("Verdana")
                    });
                buttonStyle.Setters.Add(
                    new Setter
                    {
                        Property = Control.MarginProperty,
                        Value = new Thickness(10)
                    });
                buttonStyle.Setters.Add(
                    new Setter
                    {
                        Property = Control.BackgroundProperty,
                        Value = new SolidColorBrush(Colors.Black)
                    });
                buttonStyle.Setters.Add(
                    new Setter
                    {
                        Property = Control.ForegroundProperty,
                        Value = new SolidColorBrush(Colors.White)
                    });
                buttonStyle.Setters.Add(
                    new Setter
                    {
                        Property = Control.MarginProperty,
                        Value = new Thickness(10)
                    });
                    new EventSetter
                    {
                    Event = Button.ClickEvent,
                    Handler = new RoutedEventHandler(Button_Click)
                    };

                button1.Style = buttonStyle;
                button2.Style = buttonStyle;

            }
                        

        }
        public string selectedPower = "Мощь";
        public HeroAge? selectedAge = null;
        public string selectedRace = "Раса";

        private IEnumerable<Hero> _HeroList;
        public IEnumerable<Hero> HeroList
        {
            get
            {
                var res = _HeroList;

                res = res
                    .Where(c => (c.power == selectedPower || selectedPower == "Мощь"))
                    .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
                    .Where(c => (c.race == selectedRace || selectedRace == "Раса"));
                if (searchFilter != "")
                    res = res.Where(c => c.name.IndexOf(
                        searchFilter,
                        StringComparison.OrdinalIgnoreCase) >= 0);
                if (sortAsc) res = res.OrderBy(c => c.Age);
                else res = res.OrderByDescending(c => c.Age);

                return res;

            }
            set
            {
                _HeroList = value;
            }
        }

        public List<HeroPower> HeroPowerList { get; set; }
        public List<HeroAge> HeroAgeList { get; set; }
        public List<HeroRace> HeroRaceList { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Globals.dataProvider = new JSONDataProvider();
            HeroList = Globals.dataProvider.getHero();
            HeroPowerList = Globals.dataProvider.getPower().ToList();
            HeroPowerList.Insert(0, new HeroPower { title = "Мощь" });
            HeroAgeList = Globals.dataProvider.getAge().ToList();
            HeroRaceList = Globals.dataProvider.getRace().ToList();
            HeroRaceList.Insert(0, new HeroRace { title = "Раса" });
        }

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void HeroListBox_MouseDoubleClick(
    object sender,
    MouseButtonEventArgs e)
        {
            // в создаваемое окно передаем выбранного котика
            var detailWindow = new DetailWindow(
                HeroListBox.SelectedItem as Hero);

            detailWindow.ShowDialog();
        }
        private void powerFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedPower = (powerFilterComboBox.SelectedItem as HeroPower).title;
            Invalidate();
        }
        private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedAge = AgeFilterComboBox.SelectedItem as HeroAge;
            Invalidate();
        }
        private void RaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedRace = (RaceFilterComboBox.SelectedItem as HeroRace).title;
            Invalidate();
        }
        private string searchFilter = "";

        private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
        {
            searchFilter = SearchFilterTextBox.Text;
            Invalidate();
        }
        private bool sortAsc = true;

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            sortAsc = (sender as RadioButton).Tag.ToString() == "1";
            Invalidate();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button clickedButton = (Button)sender;
            MessageBox.Show(clickedButton.Content.ToString());
        }


        public class JSONDataProvider : LocalDataProvider, IDataProvider
        {
            private List<Hero> _HeroList;

            public JSONDataProvider()
            {
                var serializer = new DataContractJsonSerializer(typeof(Hero[]));
                using (var sr = new StreamReader("./data.json"))
                {
                    _HeroList = ((Hero[])serializer.ReadObject(sr.BaseStream)).ToList();
                }
            }

            public IEnumerable<Hero> getHero()
            {
                return _HeroList;
            }


        }

    }
    interface IDataProvider
    {
        IEnumerable<Hero> getHero();
        IEnumerable<HeroPower> getPower();
        IEnumerable<HeroAge> getAge();
        IEnumerable<HeroRace> getRace();
    }


    public class LocalDataProvider : IDataProvider
    {
        public IEnumerable<HeroPower> getPower()
        {
            return new HeroPower[]
            {
                new HeroPower()
                {
                    title="1/10"

                },
                new HeroPower()
                {
                    title="2/10"
                },
                new HeroPower()
                {
                    title="3/10"

                },
                new HeroPower()
                {
                    title="4/10"

                },
                new HeroPower()
                {
                    title="5/10"

                },
                new HeroPower()
                {
                    title="6/10"

                },
                new HeroPower()
                {
                    title="7/10"

                },
                new HeroPower()
                {
                    title="8/10"

                },
                new HeroPower()
                {
                    title="9/10"

                },
                new HeroPower()
                {
                    title="10/10"

                },

            };
        }
        public IEnumerable<HeroAge> getAge()
        {
            return new HeroAge[]
            {
                new HeroAge()
                {
                    title="Все возраста",
                    AgeFrom=0,
                    AgeTo=2000

                },
                new HeroAge()
                {
                    title="Дети",
                    AgeFrom=1,
                    AgeTo=100

                },
                new HeroAge()
                {
                    title="Подростки",
                    AgeFrom=100,
                    AgeTo=500

                },
                new HeroAge()
                {
                    title="Молодые",
                    AgeFrom=500,
                    AgeTo=700

                },
                new HeroAge()
                {
                    title="Старики",
                    AgeFrom=700,
                    AgeTo=2000

                }

            };

        }
        public IEnumerable<HeroRace> getRace()
        {
            return new HeroRace[]
            {
                new HeroRace()
                {
                    title="Человек"
                },
                new HeroRace()
                {
                    title="Дракон"
                },
                new HeroRace()
                {
                    title="Фея"
                },
                new HeroRace()
                {
                    title="Огр"
                },
                new HeroRace()
                {
                    title="Рыба"
                },
                new HeroRace()
                {
                    title="Селёдка"
                },
                new HeroRace()
                {
                    title="Камень"
                },
                new HeroRace()
                {
                    title="Гоблин"
                },
            };
        }
        public IEnumerable<Hero> getHero()
        {
            return new Hero[]{
            new Hero{
                race="Человек",
                name="Invoker",
                Age= 1000,
                power="10/10"},
            new Hero{
                race="Человек",
                name="Juggernaut",
                Age= 450,
                power="9/10"},
            new Hero{
                race="Человек",
                name="lina",
                Age = 300,
                power="8/10"},
            new Hero{
                race="Огр",
                name="Troll Warloard",
                Age = 100,
                power="7/10"},
            new Hero{
                race="Рыба",
                name="Slark",
                Age = 120,
                power="6/10"},
            new Hero{
                race="Селёдка",
                name="Slardar",
                Age = 200,
                power="5/10"},
            new Hero{
                race="Камень",
                name="Tiny",
                Age = 20000,
                power="4/10"},
            new Hero{
                race="Фея",
                name="Dark Willow",
                Age = 700,
                power="3/10"},
            new Hero{
                race="Огр",
                name="Orge Magi",
                Age = 1400,
                power="2/10"},
            new Hero{
                race="Дракон",
                name="Viper",
                Age = 1900,
                power="1/10"},

        };



        }



    }

}