using DemoExam.Model;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace DemoExam.ViewModel
{
    public class ViewModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        public Entities DataBase = new Entities();
        public ObservableCollection<Agent> outPutList;


        public ObservableCollection<Agent> GetListAgent
        {
            get
            {
                if (outPutList == null)
                {
                    outPutList = new ObservableCollection<Agent>(DataBase.Agent.ToList());
                }
                return outPutList;
            }
        }



        int setSortIndex;
        public int SetSortIndex
        {
            get
            {
                return setSortIndex;
            }
            set
            {
                setSortIndex = value;

                FiltFunc(setFiltValue);
                SearchFunc(searchText);
                SortFunc(setSortIndex);
                PropertyChanged(this, new PropertyChangedEventArgs("GetListAgent"));
            }
        }

        public void SortFunc(int index)
        {
            switch (index)
            {
                case 0:
                    outPutList = new ObservableCollection<Agent>(outPutList.OrderBy(x => x.Title));
                    break;
                case 1:
                    outPutList = new ObservableCollection<Agent>(outPutList.OrderBy(x => x.GetDiscont));
                    break;
                case 2:
                    outPutList = new ObservableCollection<Agent>(outPutList.OrderBy(x => x.Priority));
                    break;
                case 3:
                    outPutList = new ObservableCollection<Agent>(outPutList.OrderBy(x => x.Title).Reverse());
                    break;
                case 4:
                    outPutList = new ObservableCollection<Agent>(outPutList.OrderBy(x => x.GetDiscont).Reverse());
                    break;
                case 5:
                    outPutList = new ObservableCollection<Agent>(outPutList.OrderBy(x => x.Priority).Reverse());
                    break;

            }

        }




        public List<string> GetListTypeAgent
        {
            get
            {
                List<string> nameAgentType = new List<string>();
                nameAgentType.Add("Все типы");
                var temp = DataBase.AgentType;
                foreach (var item in temp)
                {
                    nameAgentType.Add(item.Title);
                }
                return nameAgentType;
            }
        }

        string setFiltValue = string.Empty;
        public string SetFiltValue
        {
            get
            {
                return setFiltValue;
            }
            set
            {
                setFiltValue = value;
                FiltFunc(setFiltValue);
                SearchFunc(searchText);
                SortFunc(setSortIndex);
                PropertyChanged(this, new PropertyChangedEventArgs("GetListAgent"));
            }
        }


        public void FiltFunc(string filtString)
        {
            if (filtString == "Все типы" || filtString == "")
            {
                outPutList = new ObservableCollection<Agent>(DataBase.Agent.ToList());
            }
            else
            {
                try
                {
                    outPutList = new ObservableCollection<Agent>(outPutList.Where(x => x.AgentType.Title == filtString));
                }
                catch
                {
                    MessageBox.Show("Произшла ошибка. Перезапустите приложение для корректной работы");
                }
                
            }

        }

        string searchText = String.Empty;
        public string SearchText
        {
            get
            {
                return searchText;
            }
            set
            {
                searchText = value;
                FiltFunc(setFiltValue);
                SearchFunc(searchText);
                SortFunc(setSortIndex);
                PropertyChanged(this, new PropertyChangedEventArgs("GetListAgent"));
            }
        }

        public void SearchFunc(string text)
        {
            outPutList = new ObservableCollection<Agent>(outPutList.Where(x => x.Title.Contains(text) || x.Email.Contains(text) || x.Phone.Contains(text)));
        }


        List<Agent> selectedAgentsList = new List<Agent>();
        public void SetSelectedAgents(IList<object> selectedAgents)
        {
            selectedAgentsList = new List<Agent>();
            foreach (var item in selectedAgents)
            {
                selectedAgentsList.Add((Agent)item);
            }
            PropertyChanged(this, new PropertyChangedEventArgs("IsVisibleButton"));
        }

        public bool IsVisibleButton
        {
            get
            {
                if (selectedAgentsList.Count() > 1)
                {
                    return true;
                }
                return false;
            }
        }


        int priority = -1;
        public int GetMaxPriority
        {
            get
            {
                if (priority == -1)
                {
                    if (selectedAgentsList.Count() != 0)
                    {

                        foreach (var i in selectedAgentsList)
                        {
                            if (priority < i.Priority)
                            {
                                priority = i.Priority;
                            }
                        }
                        return priority;
                    }
                    return priority;
                }
                else
                {
                    return priority;
                }
            }

            set
            {
                priority = value;
            }

        }


        public void SetPriority(int value)
        {

            for (int i = 0; i < selectedAgentsList.Count(); i++)
            {
                selectedAgentsList[i].Priority = value;
            }

        }

        Agent selectedAgent;
        public Agent SelectedAgent
        {
            get
            {
                return selectedAgent;
            }
            set
            {
                selectedAgent = value;
            }
        }


        string selectedAgentType = string.Empty;
        public string AgentTypeSelectedAgent
        {
            get 
            {
                if (selectedAgentType == "")
                {
                    if (SelectedAgent.AgentType == null)
                    {
                        return "";
                    }
                    return SelectedAgent.AgentType.Title.ToString();
                }
                return selectedAgentType;
            }
            set
            {
                SelectedAgent.AgentTypeID = DataBase.AgentType.FirstOrDefault(x => x.Title == value).ID;
                selectedAgentType = value;
            }
        }



        public ViewModel()
        {
          

        }

    }
}
