using System;
using System.Collections.Generic;
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;
using NetSDKCS;
using System.Collections.ObjectModel;
using Helper;


namespace PlayBackDemo
{
    /// <summary>
    /// PlayBackByTimePage.xaml 的交互逻辑
    /// </summary>
    public partial class PlayBackByTimePage : Page
    {
        #region
        public int ChannelSelectIndex { get; set; }
        public int ModeSelectIndex { get; set; }
        public int TypeSelectIndex { get; set; }
        public int StreamSelectIndex { get; set; }

        private ObservableCollection<int> ChannelSource = new ObservableCollection<int>();
        #endregion

        #region
        
        #endregion

        public PlayBackByTimePage()
        {
            InitializeComponent();
            
            PB_ChannelComboBox.ItemsSource = ChannelSource;
            DL_ChannelComboBox.ItemsSource = ChannelSource;

            PB_TypeComboBox.Items.Clear();
            PB_TypeComboBox.Items.Add((string)App.Current.TryFindResource("ID_AllTYPE"));
            PB_TypeComboBox.Items.Add((string)App.Current.TryFindResource("ID_ALARMTYPE"));
            PB_TypeComboBox.Items.Add((string)App.Current.TryFindResource("ID_MOTIONTYPE"));
            PB_DatePicker.SelectedDate = DateTime.Today;
            PB_StreamComboBox.Items.Clear();
            //PB_StreamComboBox.Items.Add((string)App.Current.TryFindResource("ID_ALLSTREAM"));
            PB_StreamComboBox.Items.Add((string)App.Current.TryFindResource("ID_MAINSTREAM"));
            PB_StreamComboBox.Items.Add((string)App.Current.TryFindResource("ID_SUBSTREAM"));

            DL_StreamComboBox.Items.Clear();
            DL_StreamComboBox.Items.Add((string)App.Current.TryFindResource("ID_MAINSTREAM"));
            DL_StreamComboBox.Items.Add((string)App.Current.TryFindResource("ID_SUBSTREAM"));
        }

        public void SetChannelSource(int nChannelNum)
        {
            LogHelper.WriteWithMethod("Enter");
            ChannelSource.Clear();
            for (int i = 1; i <= nChannelNum; i++)
            {
                ChannelSource.Add(i);
            }
            PB_ChannelComboBox.SelectedIndex = 0;
            DL_ChannelComboBox.SelectedIndex = 0;
            LogHelper.WriteWithMethod("Leave");
        }

        private void DL_Button_Click(object sender, RoutedEventArgs e)
        {
            LogHelper.WriteWithMethod("Enter");
            int nChannelId;
            DateTime startTime;
            DateTime endTime;
            int nStreamType;
            //channel
            if (-1 != DL_ChannelComboBox.SelectedIndex)
            {
                nChannelId = DL_ChannelComboBox.SelectedIndex;
            }
            else
            {
                MessageBox.Show((string)App.Current.TryFindResource("ID_NOCHANNEL"));
                return;
            }

            //startTime & endTime

            startTime = DL_StartTime.Value;
            endTime = DL_EndTime.Value;
            
            

            //StreamType
            if (-1 != DL_StreamComboBox.SelectedIndex)
            {
                nStreamType = DL_StreamComboBox.SelectedIndex + 1;//0-both main and extra stream,1-only main stream,2-only extra stream
            }
            else
            {
                MessageBox.Show((string)App.Current.TryFindResource("ID_NOSTREAM"));
                return;
            }
            //set config in main window
            MainWindow mainWindow = App.Current.MainWindow as MainWindow;
            mainWindow.m_Download_Channel = nChannelId;
            mainWindow.m_Download_StartTime = startTime;
            mainWindow.m_Download_EndTime = endTime;
            mainWindow.m_Download_StreamType = nStreamType;
            mainWindow.m_Download_recordType = EM_RECORD_TYPE.ALL;
            mainWindow.Download();
            LogHelper.WriteWithMethod("Leave");
        }

        private void DL_Stop_Click(object sender, RoutedEventArgs e)
        {
            LogHelper.WriteWithMethod("Enter");
            (App.Current.MainWindow as MainWindow).StopDownLoad();
            LogHelper.WriteWithMethod("Leave");
        }

        private void PB_Play_Click(object sender, RoutedEventArgs e)
        {
            LogHelper.WriteWithMethod("Enter");
            int nChannelId;
            DateTime startTime;
            DateTime endTime;
            int nStreamType;
            EM_RECORD_TYPE emRecordType;
            //channel
            if (-1 != PB_ChannelComboBox.SelectedIndex)
            {
                nChannelId = PB_ChannelComboBox.SelectedIndex;
            }
            else
            {
                MessageBox.Show((string)App.Current.TryFindResource("ID_NOCHANNEL"));
                return;
            }

            //startTime & endTime
            if (PB_DatePicker.SelectedDate.HasValue)
            {
                startTime = PB_DatePicker.SelectedDate.Value;
                endTime = startTime.AddDays(1);
            }
            else
            {
                MessageBox.Show((string)App.Current.TryFindResource("ID_NODATE"));
                return;
            }

            //StreamType
            if (-1 != PB_StreamComboBox.SelectedIndex)
            {
                nStreamType = PB_StreamComboBox.SelectedIndex + 1;//0-both main and extra stream,1-only main stream,2-only extra stream
            }
            else
            {
                MessageBox.Show((string)App.Current.TryFindResource("ID_NOSTREAM"));
                return;
            }

            //Record_Type
            if (-1 != PB_TypeComboBox.SelectedIndex)
            {
                switch (PB_TypeComboBox.SelectedIndex)
                {
                    case 0:
                        emRecordType = EM_RECORD_TYPE.ALL;
                        break;
                    case 1:
                        emRecordType = EM_RECORD_TYPE.ALARM;
                        break;
                    case 2:
                        emRecordType = EM_RECORD_TYPE.MOTION;
                        break;
                    default:
                        emRecordType = EM_RECORD_TYPE.ALL;
                        break;
                }
            }
            else
            {
                MessageBox.Show((string)App.Current.TryFindResource("ID_NOTYPE"));
                return;
            }

            //set config in main window
            MainWindow mainWindow = App.Current.MainWindow as MainWindow;
            mainWindow.m_PlayBack_Channel = nChannelId;
            mainWindow.m_PlayBack_StartTime = startTime;
            mainWindow.m_PlayBack_EndTime = endTime;
            mainWindow.m_PlayBack_StreamType = nStreamType;
            mainWindow.m_PlayBack_recordType = emRecordType;
            mainWindow.Dispatcher.BeginInvoke((Action)(() =>
            {
                mainWindow.StopPlayBack(true);
                mainWindow.QueryTimeBlock();
                mainWindow.StartPlayBack(); 
            }));
            //mainWindow.QueryTimeBlock();
            //mainWindow.StartPlayBack();
            LogHelper.WriteWithMethod("Leave");
        }

    }
    

}
