using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Domain.Common.DataExporting;
using MWS.Domain.Dto;
using MWS.Domain.Prints;
using MWS.Domain.View.History;
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;
using System.Windows.Threading;
using Visifire.Charts;

namespace MWS.Domain.View.Weight
{
    /// <summary>
    /// WeightRecordChartPage.xaml 的交互逻辑
    /// </summary>
    public partial class WeightRecordChartPage : Page
    {
        private IWeight weightManage;
        private IDevice deviceManage;
        private ITeam teamManage;
        private IEntrance entranceManage;
        private int entranceId = 0;
        private int deviceId = 0;
        private int teamId = 0;
        private DateTime startTime;
        private DateTime endTime;
        private List<Zigbee> devices;
        private IZigbee zigbeeManager;

        private Chart chart;
        public WeightRecordChartPage()
        {
            InitializeComponent();
            weightManage = new WeightManager();
            deviceManage = new DeviceManager();
            teamManage = new TeamManager();
            entranceManage = new EntranceManager();
            zigbeeManager = new ZigbeeManager();
        }

       
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            startTime = DateTime.Now.AddHours(-24);
            endTime = DateTime.Now;
            BindEntranceComboBox();
            BindDeviceComboBox();
            BindTeamComboBox();
            //pagination.CurrentPageNumberChanging += Pagination_CurrentPageNumberChanging;
            btDataSelect.Content = startTime.ToString() + "-" + endTime.ToString();


            GetListData();
        }

        private void GetListData()
        {
            entranceId = Convert.ToInt32(entrance.SelectedValue);
            deviceId = Convert.ToInt32(device.SelectedValue);
            teamId = Convert.ToInt32(team.SelectedValue);
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            startTime = dateSelect[0].ToDateTime();
            endTime = dateSelect[1].ToDateTime();
            List<RecordDto> details = weightManage.GetAllExportRecords(entranceId, deviceId, teamId, startTime, endTime);

            CreateChartColumn("称重记录", details);
            //lstWeight.ItemsSource = pa.Data;

            //pagination.TotalDataCount = pa.TotalCount;
            //pagination.CurrentPageNumber = pageIndex;
        }
        private void search_Click(object sender, RoutedEventArgs e)
        {
            GetListData();
        }
        /// <summary>
        /// 获取料口类型
        /// </summary>
        /// <param name="selectIndex"></param>
        private void BindEntranceComboBox()
        {
            try
            {
                List<T_Entrance> entrances = entranceManage.GetAll("");

                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
                //    (entrances.Select(c=>c.Id,c)
                //{
                //    new KeyValuePair<string, string>("0", "全部"),
                //    new KeyValuePair<string, string>("1", "1号料口"),
                //    new KeyValuePair<string, string>("2", "2号料口"),
                //    new KeyValuePair<string, string>("3", "3号料口")
                // };
                foreach (var item in entrances)
                {
                    list.Add(new KeyValuePair<string, string>(item.Id.ToString(), item.Name));
                }
                list.Insert(0, new KeyValuePair<string, string>("0", "全部"));
                entrance.ItemsSource = list;
                entrance.SelectedValuePath = "Key";
                entrance.DisplayMemberPath = "Value";
                entrance.SelectedIndex = 0;
            }
            catch (Exception ex)
            {

                throw;
            }

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

                devices = zigbeeManager.GetAll();
                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
                //    (entrances.Select(c=>c.Id,c)
                //{
                //    new KeyValuePair<string, string>("0", "全部"),
                //    new KeyValuePair<string, string>("1", "1号料口"),
                //    new KeyValuePair<string, string>("2", "2号料口"),
                //    new KeyValuePair<string, string>("3", "3号料口")
                // };
                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;
            }

        }
        /// <summary>
        /// 获取班组类型
        /// </summary>
        /// <param name="selectIndex"></param>
        private void BindTeamComboBox()
        {
            try
            {
                List<Team> teams = teamManage.GetAll("");

                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
                //    (entrances.Select(c=>c.Id,c)
                //{
                //    new KeyValuePair<string, string>("0", "全部"),
                //    new KeyValuePair<string, string>("1", "1号料口"),
                //    new KeyValuePair<string, string>("2", "2号料口"),
                //    new KeyValuePair<string, string>("3", "3号料口")
                // };
                foreach (var item in teams)
                {
                    list.Add(new KeyValuePair<string, string>(item.Id.ToString(), item.Name));
                }
                list.Insert(0, new KeyValuePair<string, string>("0", "全部"));

                team.ItemsSource = list;
                team.SelectedValuePath = "Key";
                team.DisplayMemberPath = "Value";
                team.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", "夜班"),
                    new KeyValuePair<string, string>("2", "白班"),
                    new KeyValuePair<string, string>("3", "中班")
                 };
                team.ItemsSource = list;
                team.SelectedValuePath = "Key";
                team.DisplayMemberPath = "Value";
                team.SelectedIndex = 0;
            }

        }

      
        private void btDataSelect_Click(object sender, RoutedEventArgs e)
        {
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            startTime = dateSelect[0].ToDateTime();
            endTime = dateSelect[1].ToDateTime();
            DateTimeSelectWindow dateTimeSelectWindow = new DateTimeSelectWindow(startTime, endTime);
            dateTimeSelectWindow.ShowDialog();
            if ((bool)dateTimeSelectWindow.DialogResult)
            {
                this.startTime = dateTimeSelectWindow.BeginTime;
                this.endTime = dateTimeSelectWindow.EndTime;
                btDataSelect.Content = dateTimeSelectWindow.BeginTime.ToString() + "-" + dateTimeSelectWindow.EndTime.ToString();
            };
        }
        public void CreateChartColumn(string name, List<RecordDto> details)
        {
            //创建一个图标
            chart = new Chart();

            //设置图标的宽度和高度
            chart.Width = 1700;
            chart.Height = 780;
            //chart.Margin = new Thickness(50, 5, 50, 5);
            //是否启用打印和保持图片
            chart.ToolBarEnabled = true;

            //设置图标的属性
            chart.ScrollingEnabled = false;//是否启用或禁用滚动
            chart.View3D = false;//3D效果显示

            //创建一个标题的对象
            Title title = new Title();

            //设置标题的名称
            title.Text = name;
            title.Padding = new Thickness(0, 10, 5, 0);

            //向图标添加标题
            chart.Titles.Add(title);

            Axis yAxis = new Axis();
            //设置图标中Y轴的最小值永远为0           
            yAxis.AxisMinimum = 0;
            //设置图表中Y轴的后缀          
            yAxis.Suffix = "T";
            chart.AxesY.Add(yAxis);

            // 创建一个新的数据线。               
            DataSeries dataSeries = new DataSeries();

            // 设置数据线的格式
            dataSeries.RenderAs = RenderAs.Spline;
            //RenderAs图标类型： 柱形图(Column) 曲线图(Line)折线图(Step Line)饼图(Pie)横道图(Bar)区域图(Area)环形图(Doughnut)堆积柱形图(Stacked Column)100 % 堆积柱形图(100 % Stacked Column)
            //堆积道图(Stacked Bar)100 % 堆积横道图(100 % Stacked Bar)堆积区域图(Stacked Area)100 % 堆积区域图(100 Stacked Area)气泡图(Bubble)散射图(Point)+
            //流线漏斗图(StreamLineFunnel)漏斗图(Section Funnel)阴阳烛图(CandleStick)股票图(Stock)极区图(Line)雷达图(Radar)光滑曲线图(Spline)高密度曲线图(QuickLine)


            // 设置数据点              
            DataPoint dataPoint;
            foreach (var item in details)
            {
                dataPoint = new DataPoint();
                // 设置X轴点                    
                dataPoint.AxisXLabel = item.Time.ToString();
                //设置Y轴点                   
                dataPoint.YValue = double.Parse(item.NetWeight.ToString());
                //添加一个点击事件        
                //dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
                //添加数据点                   
                dataSeries.DataPoints.Add(dataPoint);
            }

            //for (int i = 0; i < valuex.Count; i++)
            //{
            //    // 创建一个数据点的实例。                   
            //    dataPoint = new DataPoint();
            //    // 设置X轴点                    
            //    dataPoint.AxisXLabel = valuex[i];
            //    //设置Y轴点                   
            //    dataPoint.YValue = double.Parse(valuey[i]);
            //    //添加一个点击事件        
            //    //dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
            //    //添加数据点                   
            //    dataSeries.DataPoints.Add(dataPoint);
            //}

            // 添加数据线到数据序列。                
            chart.Series.Add(dataSeries);

            //将生产的图表增加到Grid，然后通过Grid添加到上层Grid.           
            Grid gr = new Grid();
            gr.Children.Add(chart);
            this.RecordChart.Children.Add(gr);
        }
    }
}
