using OxyPlot.Axes;
using OxyPlot.Legends;
using OxyPlot.Series;
using OxyPlot;
using Prism.Commands;
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Mvvm;
using Repository;
using Repository.Entiies;
using System.Data;
using System.Windows.Forms;
using TickStyle = OxyPlot.Axes.TickStyle;
using AutoMapper;
using HandyControl.Data;

namespace LogoForceTestApp.Modules.MainModule.ViewModels
{
    public class StatementPageViewModel : BindableBase
    {
        public List<Information> InfNew { get; set; }
        private readonly IMapper _mapper;
        private readonly List<Information> _infNew;

        public int PageIndex { get; set; } = 1;
        public int MaxCount { get; set; } = 1;
        public int PageCount { get; set; } = 20;
        public DateTime SelectedStartDate { get; set; }
        public DateTime SelectedEndDate { get; set; }

        public DelegateCommand<FunctionEventArgs<int>> PageUpdatedCmd => new(PageUpdated);

        private readonly IRepository repository;

        public string ProCode { get; set; }
        public DelegateCommand QueryCommand { get; set; }
        public StatementPageViewModel(IMapper mapper, IRepository repository)
        {
            SelectedStartDate = DateTime.Today;
            SelectedEndDate = DateTime.Today;
            InfNew = new List<Information>();
            _infNew = new List<Information>();
            _mapper = mapper;

            //
            QueryCommand = new DelegateCommand(query);
            this.repository = repository;
        }

        /// <summary>
        /// 页码改变
        /// </summary>
        private void PageUpdated(FunctionEventArgs<int> info)
        {
            InfNew = _infNew.Skip((info.Info - 1) * PageCount).Take(PageCount).Reverse().ToList();
        }

        public double GeneralTime { get; set; }
        public int AllNumber { get; set; }
        string prono;
        private void query()
        {
            if (SelectedEndDate < SelectedStartDate || SelectedStartDate == default)
            {
                MessageBox.Show("请选择正确的日期", "提示");
                return;
            }

            _infNew.Clear();

            var endTime = SelectedEndDate.AddDays(1);
            var res = repository.GetAllQuery<Information>(c => c.ProCode == ProCode && c.CreateTime >= SelectedStartDate && c.CreateTime <= endTime);
            if (res[0].ProNo != "")
            { prono = res[0].ProNo; }
            else { prono = res[1].ProNo; }
            
            var list = repository.GetAllQuery<Information>(c => c.ProNo == prono && c.CreateTime >= SelectedStartDate && c.CreateTime <= endTime);

            list.Reverse();
            var dtos = _mapper.Map<List<Information>>(list);
            _infNew.AddRange(dtos);


            InfNew = _infNew.Take(PageCount).ToList();
            MaxCount = _infNew.Count / PageCount + 1;
        }
    }
}