using System.Windows;
using System.Windows.Controls;
using HistoryWallEditTool.JsonFile;

namespace HistoryWallEditTool.ControlsForData
{
    /// <summary>
    /// MainContainer.xaml 的互動邏輯
    /// </summary>
    public partial class MainContainer : UserControl
    {
        public static HistoryWallEventJson hweventJson;
        public static string imgFolderName => GlobalFunction.dataPath + @"HistoryImage\";
        public static int mainEventIndex = -1;
        public static int subEventIndex = -1;

        public CurrentEditLanguage curEditLan = CurrentEditLanguage.LanguageOne;

        public MainContainer(HistoryWallEventJson hweventJsonSource)
        {
            InitializeComponent();
            hweventJson = hweventJsonSource;
        }

        private void LoadDefaultDataByJson()
        {
            //載入主類別資料
            LoadMainEventData();

            if (hweventJson != null && hweventJson.EventGroups != null && hweventJson.EventGroups.Count > 0)
            {
                uxDividerLineDetailData.Opacity = 0;
            }
        }

        private void LoadDetailEventData()
        {
            DetailDataViewer ddvDetail = new DetailDataViewer(mainEventIndex, subEventIndex, curEditLan);

            uxDividerLineDetailData.Opacity = 1;
            uxDetailGrid.Children.Add(ddvDetail);
        }

        private void LoadSubEventData(EventGroup eventGroup)
        {
            EventCreator ecSub = new EventCreator(
                curEditLan,
                EventCreatorClass.SubEvent
                );

            ecSub.SelectChangeInfoEvent += new EventCreator.SelectChangeInfoDelegate(ecSub_SelectChangeInfoEvent);
            ecSub.DeleteEventInfoEvent += new EventCreator.DeleteEventInfoDelegate(ecSub_DeleteEventInfoEvent);

            //預設先載入第一個資料
            if (subEventIndex != -1 && subEventIndex < eventGroup.ListDates.Count)
            {
                ecSub.SetDefaultCheck(subEventIndex);
            }
            else if (eventGroup.ListDates.Count > 0)
            {
                ecSub.SetDefaultCheck(0);
            }

            uxSubEventGrid.Children.Add(ecSub);
        }

        private void LoadMainEventData()
        {
            uxCategoryGrid.Children.Clear();

            EventCreator ecMain = new EventCreator(
                curEditLan,
                EventCreatorClass.MainEvent
                );

            ecMain.SelectChangeInfoEvent += new EventCreator.SelectChangeInfoDelegate(ecMain_SelectChangeInfoEvent);
            ecMain.DeleteEventInfoEvent += new EventCreator.DeleteEventInfoDelegate(ecMain_DeleteEventInfoEvent);
            //預設先載入第一個資料
            if (mainEventIndex != -1 && mainEventIndex < hweventJson.EventGroups.Count)
            {
                ecMain.SetDefaultCheck(mainEventIndex);
            }
            else if (hweventJson.EventGroups.Count > 0)
            {
                ecMain.SetDefaultCheck(0);
            }

            uxCategoryGrid.Children.Add(ecMain);
        }

        private void ecSub_DeleteEventInfoEvent()
        {
            uxDetailGrid.Children.Clear();
        }

        private void ecSub_SelectChangeInfoEvent(int index)
        {
            if (index != subEventIndex)
            {
                uxDetailGrid.Children.Clear();
                subEventIndex = index;
                LoadDetailEventData();
            }
        }

        //主事件被刪除~載入預設類別資料 (資料中的第一筆)
        private void ecMain_DeleteEventInfoEvent()
        {
            uxSubEventGrid.Children.Clear();
            uxDetailGrid.Children.Clear();
            LoadDefaultDataByJson();
        }

        private void ExtraSaveMainData()
        {
            if (mainEventIndex != -1 && uxCategoryGrid.Children.Count > 0)
            {
                EventCreator ec = uxCategoryGrid.Children[0] as EventCreator;

                ec.ExtraSaveFunction(mainEventIndex);
            }
        }

        private void ExtraSaveSubData()
        {
            if (subEventIndex != -1 && uxSubEventGrid.Children.Count > 0)
            {
                EventCreator ec = uxSubEventGrid.Children[0] as EventCreator;

                ec.ExtraSaveFunction(subEventIndex);
            }
        }

        private void ecMain_SelectChangeInfoEvent(int index)
        {
            if (index != mainEventIndex)
            {
                //如果子項目不等於 -1 (代表有被選) ~ 則當主項目切換時候也必須判斷子項目是否有被修改
                ExtraSaveSubData();
                mainEventIndex = index;
                subEventIndex = -1;
                uxSubEventGrid.Children.Clear();
                uxDetailGrid.Children.Clear();
                LoadSubEventData(hweventJson.EventGroups[mainEventIndex]);
            }
        }

        #region 由主視窗(ModernWindow)來判斷該焦點是否應該存在
        public void CheckFocus()
        {
            if (uxCategoryGrid.Children.Count > 0)
            {
                EventCreator ec = uxCategoryGrid.Children[0] as EventCreator;

                if (ec != null)
                {
                    ec.MainCheckFocus();
                }
            }
            if (uxSubEventGrid.Children.Count > 0)
            {
                EventCreator ec = uxSubEventGrid.Children[0] as EventCreator;

                if (ec != null)
                {
                    ec.MainCheckFocus();
                }
            }
        }

        public void RemoveFocus()
        {
            if (uxCategoryGrid.Children.Count > 0)
            {
                EventCreator ec = uxCategoryGrid.Children[0] as EventCreator;

                if (ec != null)
                {
                    ec.MainRemoveFocus();
                }
            }
            if (uxSubEventGrid.Children.Count > 0)
            {
                EventCreator ec = uxSubEventGrid.Children[0] as EventCreator;

                if (ec != null)
                {
                    ec.MainRemoveFocus();
                }
            }
        }
        #endregion

        #region 由主視窗(Mainwindow)介面控制的函數
        //改變當前時間軸模式 (時間軸 or 分類軸)
        internal void SetCurControlLine(ControlLine controlLine)
        {
            LoadDefaultDataByJson();
        }

        //系統自動排序功能
        internal void uxArrangeLine_Click(object sender, RoutedEventArgs e)
        {
            //資料程式自動排序
            //hweventJson.EventGroups.Sort((x, y) => string.Compare(x.Year, y.Year));
            //載入預設類別資料 (資料中的第一筆)
            //LoadDefaultDataByJson();
        }

        //切換現階段要編輯的語系
        internal void ChangeEventEditLan(CurrentEditLanguage switchLan)
        {
            if (curEditLan != switchLan)
            {
                curEditLan = switchLan;

                //透過切換語系~重新載入資訊
                if (uxCategoryGrid.Children.Count > 0)
                {
                    //如果子項目不等於 -1 (代表有被選) ~ 則當主項目切換時候也必須判斷子項目是否有被修改
                    ExtraSaveMainData();
                    uxCategoryGrid.Children.Clear();
                    LoadMainEventData();
                }

                if (uxSubEventGrid.Children.Count > 0)
                {
                    //如果子項目不等於 -1 (代表有被選) ~ 則當主項目切換時候也必須判斷子項目是否有被修改
                    ExtraSaveSubData();
                    uxSubEventGrid.Children.Clear();
                    //uxDetailGrid.Children.Clear();
                    LoadSubEventData(hweventJson.EventGroups[mainEventIndex]);
                }

                if (uxDetailGrid.Children.Count > 0)
                {
                    DetailDataViewer ddv = uxDetailGrid.Children[0] as DetailDataViewer;

                    ddv.LoadOtherLanDataByLang(curEditLan);
                }
            }
        }
        #endregion 

        #region 儲存資料到json
        //失去焦點~明確的將當前的資料存起來
        internal void SaveCurDataToJson()
        {
            if (mainEventIndex != -1 && uxCategoryGrid.Children.Count > 0)
            {
                EventCreator ec = uxCategoryGrid.Children[0] as EventCreator;

                ec.uxItemListBox.SelectedIndex = -1;
            }

            if (subEventIndex != -1 && uxSubEventGrid.Children.Count > 0)
            {
                EventCreator ec = uxSubEventGrid.Children[0] as EventCreator;

                ec.uxItemListBox.SelectedIndex = -1;
            }
        }
        #endregion 
    }
}
