using LogoForceTestApp.Modules.MainModule.Models;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Repository;
using Repository.Entiies;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LogoForceTestApp.Modules.MainModule.ViewModels
{
    public class AccountPageViewModel: BindableBase
    {
        private readonly IContainerProvider _container;

        #region 属性

        private MulProductModel _Products;
        public MulProductModel Products
        {
            get { return _Products; }
            set { SetProperty(ref _Products, value); }
        }

       
        private string _PTName;
        public string PTName
        {
            get { return _PTName; }
            set { SetProperty(ref _PTName, value); }
        }
        private int _PTNameNumber;
        public int PTNameNumber
        {
            get { return _PTNameNumber; }
            set { SetProperty(ref _PTNameNumber, value); }
        }
        

        private ObservableCollection<string> _Question1=new ObservableCollection<string>();
        public ObservableCollection<string> Question1
        {
            get { return _Question1; }
            set { SetProperty(ref _Question1, value); }
        }

        private ObservableCollection<string> _Question2 = new ObservableCollection<string>();
        public ObservableCollection<string> Question2
        {
            get { return _Question2; }
            set { SetProperty(ref _Question2, value); }
        }

        private ObservableCollection<string> _Question3 = new ObservableCollection<string>();
        public ObservableCollection<string> Question3
        {
            get { return _Question3; }
            set { SetProperty(ref _Question3, value); }
        }

        private int _Selected1;
        public int Selected1
        {
            get { return _Selected1; }
            set { SetProperty(ref _Selected1, value); }
        }

        private int _Selected2;
        public int Selected2
        {
            get { return _Selected2; }
            set { SetProperty(ref _Selected2, value); }
        }

        private int _Selected3;
        public int Selected3
        {
            get { return _Selected3; }
            set { SetProperty(ref _Selected3, value); }
        }

        private string _Text1;
        public string Text1
        {
            get { return _Text1; }
            set { SetProperty(ref _Text1, value); }
        }

        private string _Text2;
        public string Text2
        {
            get { return _Text2; }
            set { SetProperty(ref _Text2, value); }
        }

        private string _Text3;
        public string Text3
        {
            get { return _Text3; }
            set { SetProperty(ref _Text3, value); }
        }
        #endregion

        #region 命令
        public DelegateCommand AddCmm { get; set; }
        public DelegateCommand AcquireCmm { get; set; }
        public DelegateCommand ModificationCmm { get; set; }


        private DelegateCommand _RemoveQuestion1Command;
        public DelegateCommand RemoveQuestion1Command =>
            _RemoveQuestion1Command ?? (_RemoveQuestion1Command = new DelegateCommand(ExecuteRemoveQuestion1Command));

        private DelegateCommand _RemoveQuestion2Command;
        public DelegateCommand RemoveQuestion2Command =>
            _RemoveQuestion2Command ?? (_RemoveQuestion2Command = new DelegateCommand(ExecuteRemoveQuestion2Command));

        private DelegateCommand _RemoveQuestion3Command;
        public DelegateCommand RemoveQuestion3Command =>
            _RemoveQuestion3Command ?? (_RemoveQuestion3Command = new DelegateCommand(ExecuteRemoveQuestion3Command));
        
        private DelegateCommand<string> _AddQuesetion1;
        public DelegateCommand<string> AddQuesetion1 =>
            _AddQuesetion1 ?? (_AddQuesetion1 = new DelegateCommand<string>(ExecuteAddQuesetion1));
        private DelegateCommand<string> _AddQuesetion2;
        public DelegateCommand<string> AddQuesetion2 =>
            _AddQuesetion2 ?? (_AddQuesetion2 = new DelegateCommand<string>(ExecuteAddQuesetion2));
        private DelegateCommand<string> _AddQuesetion3;
        public DelegateCommand<string> AddQuesetion3 =>
            _AddQuesetion3 ?? (_AddQuesetion3 = new DelegateCommand<string>(ExecuteAddQuesetion3));

        
        #endregion

        public AccountPageViewModel(IContainerProvider container) 
        {
            _container = container;

            AddCmm = new DelegateCommand(AddProCmm);
            AcquireCmm = new DelegateCommand(AcquireProCmm);
            ModificationCmm = new DelegateCommand(ModificationProCmm);

            Products = _container.Resolve<MulProductModel>();
            if (Products == null)
            {
                Products = new MulProductModel();
            }
        }

        private void ModificationProCmm()
        {
            var currentProduct = Products.ProductCollection.FirstOrDefault(p => p.Name == PTName);
            if (currentProduct != null)
            {
                currentProduct.NameNumber = PTNameNumber;
                currentProduct.QuestionType1= new ObservableCollection<string>(Question1);
                currentProduct.QuestionType2= new ObservableCollection<string>(Question2);
                currentProduct.QuestionType3= new ObservableCollection<string>(Question3);
            }
            Products.SaveParame();
            MessageBox.Show("修改成功");
        }

        private void AcquireProCmm()
        {
            Products.LoadParame();
            var currentProduct = Products.ProductCollection.FirstOrDefault(p => p.Name == PTName);
            if (currentProduct != null)
            {
                PTNameNumber = currentProduct.NameNumber;
                Question1 = currentProduct.QuestionType1;
                Question2 = currentProduct.QuestionType2;
                Question3 = currentProduct.QuestionType3;
            }
        }

        private void AddProCmm()
        {
            if (Products.ProductCollection.Count != 0)
            {
                Products.LoadParame();
            }
            Products.ProductCollection.Add(new ProductModel() { Name = PTName, NameNumber=PTNameNumber,
                QuestionType1 = Question1, QuestionType2 = Question2, QuestionType3 = Question3 });
            Products.SaveParame();
            MessageBox.Show("添加成功");
        }

        void ExecuteRemoveQuestion3Command()
        {
            if (Selected3 < 0) return;
            Question3.RemoveAt(Selected3);
        }
        void ExecuteRemoveQuestion2Command()
        {
            if (Selected2 < 0) return;
            Question2.RemoveAt(Selected2);
        }
        void ExecuteRemoveQuestion1Command()
        {
            if (Selected1 < 0) return;
            Question1.RemoveAt(Selected1);
        }
        private void ExecuteAddQuesetion3(string str)
        {
            Question3.Add(str);
            Text3 = string.Empty;
        }
        private void ExecuteAddQuesetion2(string str)
        {
            Question2.Add(str);
            Text2 = string.Empty;
        }
        private void ExecuteAddQuesetion1(string str)
        {
            Question1.Add(str);
            Text1 = string.Empty;
        }
    }
}