using MaterialDesignThemes.Wpf;
using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Domain.ViewModel.HistoryAlertPage;
using MWS.Library.Common;
using MWS.Library.Common.Module;
using MWS.Model;
using MWS.Model.Dto;
using MWSDomain.Common.CoreLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace MWS.Domain.View.History
{
    /// <summary>
    /// HistoryReportPage.xaml 的交互逻辑
    /// </summary>
    public partial class HistoryReportPage : Page
    {
        private IDevice deviceManage;
        private IRecord recordManage;
        private long startTime;
        private long endTime;
        private DateTime beginTime;
        private DateTime endTimes;
        //private List<T_DeviceTag> t_DeviceTags;
        private int deviceId = 0;
        private HistoryReportViewModel historyReportViewModel;
        public HistoryReportPage()
        {
            InitializeComponent();
            historyReportViewModel = new HistoryReportViewModel(this);
            DataContext = historyReportViewModel;
            deviceManage = new DeviceManager();
            recordManage = new RecordManager();
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            beginTime = DateTime.Now.AddHours(-2);
            endTimes = DateTime.Now;
            BindDeviceComboBox();
            pagination.CurrentPageNumberChanging += Pagination_CurrentPageNumberChanging;
            btDataSelect.Content= beginTime.ToString() + "-" + endTimes.ToString();

            //GetListData();
        }
        private void Pagination_CurrentPageNumberChanging(object sender, RoutedEventArgs e)
        {
            
            GetListData(pagination.CurrentPageNumber);
        }
        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {

        }
        private void search_Click(object sender, RoutedEventArgs e)
        {
            if (historyReportViewModel.DeviceTags!=null&&historyReportViewModel.DeviceTags.Count>0)
            {
                GetListData();
            }
            else
            {
                Msg.Info("请先选择变量!");
            }
        }
        private void GetListData(int pageIndex=1)
        {
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            beginTime = dateSelect[0].ToDateTime();
            endTimes = dateSelect[1].ToDateTime();
            startTime = TypeExtension.GetTimeStamp(beginTime);
            endTime = TypeExtension.GetTimeStamp(endTimes);
            int[] tagids = historyReportViewModel.DeviceTags.Select(c => c.Id).ToArray();
            PageData<TagRecordDto> pd = recordManage.GetTagRecord(tagids, startTime, endTime, pageIndex, pagination.PageDataCount);
            if (pd.Data!=null&&pd.Data.Count>0)
            {
                historyReportViewModel.TagRecordDtos = pd.Data.ToList();
            }
            pagination.TotalDataCount = pd.TotalCount;
            pagination.CurrentPageNumber = pageIndex;
        }
        private void selectVar_Click(object sender, RoutedEventArgs e)
        {
            deviceId = Convert.ToInt32(device.SelectedValue);
            SelectVarCodeWindow selectVarCodeWindow = new SelectVarCodeWindow(deviceId);
            selectVarCodeWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //DialogHost.Show(selectVarCodeWindow, "RootDialog");
            selectVarCodeWindow.ShowDialog();
            if (selectVarCodeWindow.selectedTags != null&& selectVarCodeWindow.selectedTags.Count>0)
            {
                historyReportViewModel.DeviceTags = selectVarCodeWindow.selectedTags;
                GetListData();
            }

        }
        /// <summary>
        /// 获取车辆
        /// </summary>
        /// <param name="selectIndex"></param>
        private void BindDeviceComboBox()
        {
            try
            {
                List<T_Device> devices = deviceManage.GetAll();

                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
              
                foreach (var item in devices)
                {
                    list.Add(new KeyValuePair<string, string>(item.Id.ToString(), item.Name));
                }
                list.Insert(0, new KeyValuePair<string, string>("0", "全部"));

                device.ItemsSource = list;
                device.SelectedValuePath = "Key";
                device.DisplayMemberPath = "Value";
                device.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>
                {
                     new KeyValuePair<string, string>("0", "全部"),
                    new KeyValuePair<string, string>("1", "1号车"),
                    new KeyValuePair<string, string>("2", "2号车"),
                    new KeyValuePair<string, string>("3", "3号车")
                 };
                device.ItemsSource = list;
                device.SelectedValuePath = "Key";
                device.DisplayMemberPath = "Value";
                device.SelectedIndex = 0;
            }

        }

        //private void btDateTime_Click(object sender, RoutedEventArgs e)
        //{
        //    var dateSelect = btDataSelect.Content.ToString().Split('-');
        //    beginTime = dateSelect[0].ToDateTime();
        //    endTimes = dateSelect[1].ToDateTime();
        //    DateTimeSelectWindow dateTimeSelectWindow = new DateTimeSelectWindow(beginTime, endTimes);
        //    dateTimeSelectWindow.ShowDialog();
        //    if ((bool)dateTimeSelectWindow.DialogResult)
        //    {
        //        this.startTime = TypeExtension.GetTimeStamp(dateTimeSelectWindow.BeginTime); 
        //        this.endTime = TypeExtension.GetTimeStamp(dateTimeSelectWindow.EndTime);
        //        //dpBeginTime.Text = dateTimeSelectWindow.BeginTime.ToString();
        //        //dpEndTime.Text = dateTimeSelectWindow.EndTime.ToString();
        //        btDataSelect.Content = dateTimeSelectWindow.BeginTime.ToString() + "-" + dateTimeSelectWindow.EndTime.ToString();
        //        //BtnDateTime.Content = $"{startTime.ToString()} - {endTime.ToString()}";
        //    };
        //}

     

        //private void tbDateTimeSelect_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        //{
        //    var dateSelect = btDataSelect.Content.ToString().Split('-');
        //    beginTime = dateSelect[0].ToDateTime();
        //    endTimes = dateSelect[1].ToDateTime();
        //    DateTimeSelectWindow dateTimeSelectWindow = new DateTimeSelectWindow(beginTime, endTimes);
        //    dateTimeSelectWindow.ShowDialog();
        //    if ((bool)dateTimeSelectWindow.DialogResult)
        //    {
        //        this.startTime = TypeExtension.GetTimeStamp(dateTimeSelectWindow.BeginTime);
        //        this.endTime = TypeExtension.GetTimeStamp(dateTimeSelectWindow.EndTime);
        //        //dpBeginTime.Text = dateTimeSelectWindow.BeginTime.ToString();
        //        //dpEndTime.Text = dateTimeSelectWindow.EndTime.ToString();
        //        btDataSelect.Content = dateTimeSelectWindow.BeginTime.ToString() + "-" + dateTimeSelectWindow.EndTime.ToString();
        //        //BtnDateTime.Content = $"{startTime.ToString()} - {endTime.ToString()}";
        //    };
        //}

        private void btDataSelect_Click(object sender, RoutedEventArgs e)
        {
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            beginTime = dateSelect[0].ToDateTime();
            endTimes = dateSelect[1].ToDateTime();
            DateTimeSelectWindow dateTimeSelectWindow = new DateTimeSelectWindow(beginTime, endTimes);
            dateTimeSelectWindow.ShowDialog();
            if ((bool)dateTimeSelectWindow.DialogResult)
            {
                this.startTime = TypeExtension.GetTimeStamp(dateTimeSelectWindow.BeginTime);
                this.endTime = TypeExtension.GetTimeStamp(dateTimeSelectWindow.EndTime);
                //dpBeginTime.Text = dateTimeSelectWindow.BeginTime.ToString();
                //dpEndTime.Text = dateTimeSelectWindow.EndTime.ToString();
                btDataSelect.Content = dateTimeSelectWindow.BeginTime.ToString() + "-" + dateTimeSelectWindow.EndTime.ToString();
                //BtnDateTime.Content = $"{startTime.ToString()} - {endTime.ToString()}";
            };
        }
    }
}
