using FLY.OBJComponents.Client;
using FLY.Weight.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
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 FLY.Weight.UI.Client
{
    /// <summary>
    /// Page_Flow.xaml 的交互逻辑
    /// </summary>
    public partial class Page_FlowTable : Page
    {
        
        FLY.Weight.Client.WeightSystemClient mWeighterCsService;

        BufferWindow<FlyData_Flow> mWindow;


        public Page_FlowTable()
        {
            InitializeComponent();
        }


        public void Init()
        {
            mWeighterCsService = TDGage.Current.mWeighterCsService;
            itemsControl.DataContext = mWeighterCsService;


            mWindow = new BufferWindow<FlyData_Flow>(mWeighterCsService.FlowList);
            Misc.BindingOperations.SetBinding(SysParam.Current, "WindowSize", mWindow, "Size", Misc.BindingOperations.BindingMode.TwoWay);
            
            //界面........................................

            InitDataGrid();

            grid_window.DataContext = mWindow;
            stackpanel_weight.DataContext = mWeighterCsService;
        }
        void InitDataGrid()
        {
            DataGridTextColumn c = new DataGridTextColumn()
            {
                CanUserSort = false,
                CanUserReorder = false,

                Header = "时间",
                Binding = new Binding("Time") { StringFormat = "{0:MM/dd HH:mm}" }
            };
            gridFlows.Columns.Add(c);

            c = new DataGridTextColumn()
            {
                CanUserSort = false,
                CanUserReorder = false,
                Header = "总流量 kg/h",
                Binding = new Binding("Total") { StringFormat = "{0:F1}" }
            };
            gridFlows.Columns.Add(c);

            for (int i = 0; i < mWeighterCsService.ItemsCnt; i++)
            {
                string name = (char)('A' + i) + "层";
                c = new DataGridTextColumn()
                {
                    CanUserSort = false,
                    CanUserReorder = false,
                    Header = name + "流量 kg/h",
                    Binding = new Binding("Items[" + i.ToString() + "].Flow") { StringFormat = "{0:F1}" }
                };
                gridFlows.Columns.Add(c);

                c = new DataGridTextColumn()
                {
                    CanUserSort = false,
                    CanUserReorder = false,
                    Header = name + "比例 %",
                    Binding = new Binding("Items[" + i.ToString() + "].ScrewPDisp") { StringFormat = "{0:F1}" }
                };
                gridFlows.Columns.Add(c);

                c = new DataGridTextColumn()
                {
                    CanUserSort = false,
                    CanUserReorder = false,
                    Header = name + "频率 Hz",
                    Binding = new Binding("Items[" + i.ToString() + "].ScrewMotorFreq") { StringFormat = "{0:F1}" }
                };
                gridFlows.Columns.Add(c);
            }
        }
        private void button_back_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
            mWindow.Dispose();
        }

        private void button_mixtable_Click(object sender, RoutedEventArgs e)
        {
            NavigationService ns = Application.Current.Properties["NavigationService"] as NavigationService;
            if (ns != null)
            {
                Page_MixTable p = new Page_MixTable();
                WeighterC weight = ((Button)sender).Tag as WeighterC;
                p.Init(weight);
                ns.Navigate(p);
            }
        }

        private void button_clear_click(object sender, RoutedEventArgs e)
        {
            if (FLY.ControlLibrary.MyMessageBox.Show("确定是否清空全部历史数据?") == true)
            {
                mWindow.Buffer.Reset();

                FLY.ControlLibrary.Window_Tip.Show("成功", "清除完毕!", TimeSpan.FromSeconds(2));
            }
        }

        private void button_prepage_Click(object sender, RoutedEventArgs e)
        {
            mWindow.MovePrePage();
        }

        private void button_nextpage_Click(object sender, RoutedEventArgs e)
        {
            mWindow.MoveNextPage();
        }

        private void button_newest_Click(object sender, RoutedEventArgs e)
        {
            mWindow.MoveNewest();
        }
    }
}