using KernelViewerCtrlDll;
using MediaGalleryDll;
using System;
using System.Collections.Generic;
using System.IO;
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;

namespace HistoryDLL
{
    /// <summary>
    /// DetailViewer.xaml 的互動邏輯
    /// </summary>
    public partial class DetailViewer : UserControl
    {
        public delegate void ChangeLanguageBtnTriggerDelegate(int index, int useLangIndex);
        public event ChangeLanguageBtnTriggerDelegate ChangeLanguageBtnTriggerEvent;

        public delegate void CloseBtnTriggerDelegate(int indexOfCtrl);
        public event CloseBtnTriggerDelegate CloseBtnTriggerEvent;

        private XmlData xmlDatas;
        private int dataIndex = 0;
        private int eventIndex = 0;
        private int mediaIndex = 0;

        private double itemWidth = 85;
        private double itemHeight = 85;

        private LangIndex curDisLan = LangIndex.Language1;

        public int IndexOfControl = -1;

        public bool isPlayVideo = false;
        /// <summary>
        /// 建構
        /// </summary>
        /// <param name="xmlData">資料</param>
        /// <param name="dataIndex">參數一資料的索引值，改變語系時通知上層所傳的 Index</param>
        /// <param name="eventIndex">該資料的第幾個事件</param>
        /// <param name="picIndex">該事件的第幾張媒體檔</param>
        public DetailViewer(XmlData xmlData, int dataIndex, int eventIndex, int picIndex)
        {
            InitializeComponent();

            if (GlobalFunction.isSolutionUsing4K)
            {
                this.Width *= 2;
                this.Height *= 2;
                uxFirstLayer1.Height = new GridLength(20 * 2);
                uxFirstLayer2.Height = new GridLength(30 * 2);
                uxFirstLayer3.Height = new GridLength(85 * 2);
                uxFirstLayer4.Height = new GridLength(60 * 2);

                uxTopLayer1.Width = new GridLength(20 * 2);
                uxTopLayer2.Width = new GridLength(20 * 2);
                uxTopLayer3.Width = new GridLength(275 * 2);
                uxTopLayer4.Width = new GridLength(20 * 2);

                uxImgLayer.Height = new GridLength(310 * 2);

                uxGalleryGd.Margin = new Thickness(uxGalleryGd.Margin.Left * 2, 0, uxGalleryGd.Margin.Right * 2, 0);

                uxLan0.Width *= 2;
                uxLan1.Width *= 2;
                uxLan2.Width *= 2;
                uxLan3.Width *= 2;

                uxLan0.Height *= 2;
                uxLan1.Height *= 2;
                uxLan2.Height *= 2;
                uxLan3.Height *= 2;

                uxLan0.FontSize *= 1.5;
                uxLan1.FontSize *= 1.5;
                uxLan2.FontSize *= 1.5;
                uxLan3.FontSize *= 1.5;

                uxExitBtn.Width *= 2;
                uxExitBtn.Height *= 2;
                uxExitBtn.FontSize *= 1.5;

                uxLan0.Margin = uxLan1.Margin = uxLan2.Margin = uxLan3.Margin = uxExitBtn.Margin = new Thickness(0, 0, 10 * 2, 0);

                uxTitleText.Width *= 2;
                uxTitleText.Height *= 2;

                uxStrMainScroll.Width *= 2;
                uxStrMainScroll.Height *= 2;

                uxDateText.Width *= 2;
                uxDateText.Height *= 2;

                uxContentText.Width *= 2;
                uxContentText.Height *= 2;

                uxTitleText.FontSize *= 1.5;
                uxDateText.FontSize *= 1.5;
                uxContentText.FontSize *= 1.5;
            }

            this.xmlDatas = xmlData;
            this.dataIndex = dataIndex;
            this.eventIndex = eventIndex;
            this.mediaIndex = picIndex;
            //this.curDisLan = (LangIndex)ConfigSettingClass.MainBackCollect.DefaultLangIndex;

            if (this.mediaIndex < 0)
                this.mediaIndex = 0;

            this.Loaded += DetailViewer_Loaded;
        }

        private List<string> GetEventFirstFileMap()
        {
            List<string> result = new List<string>();

            for (int i = 0; i < xmlDatas.monthclassList.Count; i++)
            {
                if (xmlDatas.monthclassList[i].imagepath != "" && Directory.Exists(GlobalFunction.EventFileImgPath + xmlDatas.monthclassList[i].imagepath))
                {
                    string[] files = Directory.GetFiles(GlobalFunction.EventFileImgPath + xmlDatas.monthclassList[i].imagepath);
                    foreach (string file in files)
                    {
                        if (GlobalFunction.imgDimList.Contains(System.IO.Path.GetExtension(file).ToLower()) ||
                            GlobalFunction.mediaDimList.Contains(System.IO.Path.GetExtension(file).ToLower()))
                        {
                            result.Add(file);
                            break;
                        }
                    }
                }
                else
                {
                    // 沒有圖片~ 則載入 Logo
                    result.Add(GlobalFunction.thumbPath + ConfigSettingClass.MainBackCollect.ImgLogo);
                }
            }

            return result;
        }

        private void InitialLayoutColor()
        {
            // 背景
            uxDetailviewerGd.Background = new SolidColorBrush(ConfigSettingClass.EventContentCollect.BackgroundColor);

            // 文字顏色
            uxTitleText.Foreground = new SolidColorBrush(ConfigSettingClass.EventContentCollect.TextColor);
            uxDateText.Foreground = new SolidColorBrush(ConfigSettingClass.EventContentCollect.TextColor);
            uxContentText.Foreground = new SolidColorBrush(ConfigSettingClass.EventContentCollect.TextColor);

            // 切換語系的 Radiobtn
            for (int i = 0; i < ConfigSettingClass.MainBackCollect.LangText.Count(); i++)
            {
                RadioButton radiobtn = FindName("uxLan" + i) as RadioButton;

                radiobtn.Content = ConfigSettingClass.MainBackCollect.LangText[i];
                radiobtn.Background = new SolidColorBrush(ConfigSettingClass.BtnSelectCollect.BackgroundColorUnselect);
                radiobtn.Foreground = new SolidColorBrush(ConfigSettingClass.BtnSelectCollect.TextColorUnselect);

                if (ConfigSettingClass.MainBackCollect.LangText[i] == "")
                {
                    radiobtn.Visibility = Visibility.Collapsed;
                }
            }

            // 關閉按鈕
            uxExitBtn.Background = new SolidColorBrush(ConfigSettingClass.BtnSelectCollect.BackgroundColor);
            uxExitBtn.Foreground = new SolidColorBrush(ConfigSettingClass.BtnSelectCollect.TextColor);
        }

        private void DetailViewer_Loaded(object sender, RoutedEventArgs e)
        {
            InitialLayoutColor();

            // 載入小圖列表 (取得每個事件的第一張圖)
            List<string> eventFileList = GetEventFirstFileMap();

            MediaGalleryCtrl mediaList = new MediaGalleryCtrl(eventFileList, eventIndex, ConfigSettingClass.EventContentCollect.ThicknessColor);

            mediaList.Width = 770;
            mediaList.Height = 85;
            mediaList.CallbackMediaFileNameEvent += MediaList_CallbackMediaFileNameEvent;
            mediaList.ItemWidth = itemWidth;
            mediaList.ItemHeight = itemHeight;
            if (GlobalFunction.isSolutionUsing4K)
            {
                mediaList.Width *= 2;
                mediaList.Height *= 2;
                mediaList.ItemWidth *= 2;
                mediaList.ItemHeight *= 2;
            }

            uxGalleryGd.Children.Add(mediaList);

            switch (ConfigSettingClass.MainBackCollect.DefaultLangIndex)
            {
                case 0:
                    uxLan0.IsChecked = true;
                    break;
                case 1:
                    uxLan1.IsChecked = true;
                    break;
                case 2:
                    uxLan2.IsChecked = true;
                    break;
                case 3:
                    uxLan3.IsChecked = true;
                    break;
            }

            // 載入圖片
            LoadEventInforMedia();
        }

        // 由 MediaGalleryDll 傳出來的事件，重新載入圖片
        private void MediaList_CallbackMediaFileNameEvent(int selectedIndex)
        {
            if (eventIndex != selectedIndex)
            {
                eventIndex = selectedIndex;
                // 重載入文字
                LoadEventInforStr();
                // 重新載入圖片
                LoadEventInforMedia();
            }
        }

        private void LoadEventInforMedia()
        {
            uxMainMediaShowGd.Children.Clear();

            Dviewer dviewer = new Dviewer(GlobalFunction.EventFileImgPath + xmlDatas.monthclassList[eventIndex].imagepath, mediaIndex, GlobalFunction.isSolutionUsing4K, GlobalFunction.thumbPath + ConfigSettingClass.MainBackCollect.ImgLogo);

            mediaIndex = 0;
            dviewer.CallBackFileCountEvent += Dviewer_CallBackFileCountEvent;
            dviewer.CallBackDisplayIndexEvent += Dviewer_CallBackDisplayIndexEvent;
            dviewer.CurIsPlaingVideoEvent += Dviewer_CurIsPlaingVideoEvent;
            uxMainMediaShowGd.Children.Add(dviewer);
        }

        private void Dviewer_CurIsPlaingVideoEvent(bool isPlayVideo)
        {
            this.isPlayVideo = isPlayVideo;
        }

        private void Dviewer_CallBackDisplayIndexEvent(int Index)
        {
            for (int i = 0; i < uxImgTagList.Children.Count; i++)
            {
                (uxImgTagList.Children[i] as DetailViewerDot).uxLabel.IsEnabled = false;
            }

            if (Index < uxImgTagList.Children.Count)
            {
                (uxImgTagList.Children[Index] as DetailViewerDot).uxLabel.IsEnabled = true;
            }
        }

        private void Dviewer_CallBackFileCountEvent(int realCount)
        {
            uxImgTagList.Children.Clear();

            for (int i = 0; i < realCount; i++)
            {
                DetailViewerDot dot = new DetailViewerDot(ConfigSettingClass.EventContentCollect.ThicknessColor);

                if (i >= 20)
                    break;
                uxImgTagList.Children.Add(dot);
            }
        }

        private void LoadEventInforStr()
        {
            if (eventIndex < ConfigSettingClass.MainBackCollect.LangFlowDir.Length)
            {
                uxTitleText.FlowDirection = ConfigSettingClass.MainBackCollect.LangFlowDir[eventIndex];
                uxDateText.FlowDirection = ConfigSettingClass.MainBackCollect.LangFlowDir[eventIndex];
                uxContentText.FlowDirection = ConfigSettingClass.MainBackCollect.LangFlowDir[eventIndex];
            }

            switch (curDisLan)
            {
                case LangIndex.Language1:
                    uxTitleText.Text = xmlDatas.monthclassList[eventIndex].titleLan1;
                    uxContentText.Text = xmlDatas.monthclassList[eventIndex].Language1;
                    break;
                case LangIndex.Language2:
                    uxTitleText.Text = xmlDatas.monthclassList[eventIndex].titleLan2;
                    uxContentText.Text = xmlDatas.monthclassList[eventIndex].Language2;
                    break;
                case LangIndex.Language3:
                    uxTitleText.Text = xmlDatas.monthclassList[eventIndex].titleLan3;
                    uxContentText.Text = xmlDatas.monthclassList[eventIndex].Language3;
                    break;
                case LangIndex.Language4:
                    uxTitleText.Text = xmlDatas.monthclassList[eventIndex].titleLan4;
                    uxContentText.Text = xmlDatas.monthclassList[eventIndex].Language4;
                    break;
            }

            if (xmlDatas.monthclassList[eventIndex].month == null || xmlDatas.monthclassList[eventIndex].month == "")
                uxDateText.Visibility = Visibility.Collapsed;
            else
                uxDateText.Text = xmlDatas.monthclassList[eventIndex].month;
        }

        private void uxBoundGrid_ManipulationBoundaryFeedback(object sender, System.Windows.Input.ManipulationBoundaryFeedbackEventArgs e)
        {
            e.Handled = true;
        }

        private void uxCloseBtn_Click(object sender, RoutedEventArgs e)
        {
            if (CloseBtnTriggerEvent != null)
                this.Dispatcher.BeginInvoke(CloseBtnTriggerEvent, DispatcherPriority.Background, IndexOfControl);
        }

        private void uxLan_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton radiobtnDisable = FindName("uxLan" + (int)this.curDisLan) as RadioButton;

            radiobtnDisable.Background = new SolidColorBrush(ConfigSettingClass.BtnSelectCollect.BackgroundColorUnselect);
            radiobtnDisable.Foreground = new SolidColorBrush(ConfigSettingClass.BtnSelectCollect.TextColorUnselect);

            this.curDisLan = (LangIndex)int.Parse((sender as RadioButton).Tag.ToString());
            RadioButton radiobtnEnable = FindName("uxLan" + (int)this.curDisLan) as RadioButton;

            radiobtnEnable.Background = new SolidColorBrush(ConfigSettingClass.BtnSelectCollect.BackgroundColor);
            radiobtnEnable.Foreground = new SolidColorBrush(ConfigSettingClass.BtnSelectCollect.TextColor);
            // 重載入文字
            LoadEventInforStr();
        }
    }

    public enum LangIndex
    {
        Language1 = 0,
        Language2 = 1,
        Language3 = 2,
        Language4 = 3
    }
}
