using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace HistoryWallEditTool.ControlsForData
{
    /// <summary>
    /// UserEventItem.xaml 的互動邏輯
    /// </summary>
    public partial class UserEventItem : UserControl
    {
        private bool IsMouseDown = false;
        private DispatcherTimer myClickWaitTimer = new DispatcherTimer();
        private int checkClickDelayTime = 500;

        private ControlMode curControlMode = ControlMode.Edit;
        private string defaultAddStr = "類別 / 年代項目";

        private string displayText = "";
        public string DisplayText
        {
            get { return displayText; }
            set { displayText = value; }
        }

        public TextBlock textBlock = new TextBlock();
        public TextBox textBox = new TextBox();
        private EventCreatorClass curEventCreatorClass;
        private CurrentEditLanguage curLanguage;
        private FlowDirection curFlowDir = FlowDirection.LeftToRight;

        public UserEventItem(ControlMode controlMode, EventCreatorClass eventCreatorClass, int index, CurrentEditLanguage cel, FlowDirection flowDir)
        {
            InitializeComponent();

            #region 載入資訊
            curControlMode = controlMode;
            curEventCreatorClass = eventCreatorClass;
            curLanguage = cel;
            curFlowDir = flowDir;

            uxNumber.Text = (index + 1).ToString();
            if (curEventCreatorClass == EventCreatorClass.MainEvent)
            {
                if (curLanguage == CurrentEditLanguage.LanguageOne)
                {
                    DisplayText = MainContainer.hweventJson.EventGroups[index].YearLan1;
                }
                else if (curLanguage == CurrentEditLanguage.LanguageTwo)
                {
                    DisplayText = MainContainer.hweventJson.EventGroups[index].YearLan2;
                }
                else if (curLanguage == CurrentEditLanguage.LanguageThree)
                {
                    DisplayText = MainContainer.hweventJson.EventGroups[index].YearLan3;
                }
                else if (curLanguage == CurrentEditLanguage.LanguageFour)
                {
                    DisplayText = MainContainer.hweventJson.EventGroups[index].YearLan4;
                }
            }
            else if (curEventCreatorClass == EventCreatorClass.SubEvent)
            {
                if (curLanguage == CurrentEditLanguage.LanguageOne)
                {
                    DisplayText = MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].ListDates[index].TitleLan1;
                }
                else if (curLanguage == CurrentEditLanguage.LanguageTwo)
                {
                    DisplayText = MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].ListDates[index].TitleLan2;
                }
                else if (curLanguage == CurrentEditLanguage.LanguageThree)
                {
                    DisplayText = MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].ListDates[index].TitleLan3;
                }
                else if (curLanguage == CurrentEditLanguage.LanguageFour)
                {
                    DisplayText = MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].ListDates[index].TitleLan4;
                }
            }
            #endregion

            InitialCommonControl();

            myClickWaitTimer.Interval = TimeSpan.FromMilliseconds(checkClickDelayTime);
            myClickWaitTimer.Tick += new EventHandler(mouseWaitTimer_Tick);

            this.Loaded += new RoutedEventHandler(ControllerWithClickAndDoubleClick_Loaded);
        }

        private void SaveDataToBuffer()
        {
            if (curEventCreatorClass == EventCreatorClass.MainEvent)
            {
                if (curLanguage == CurrentEditLanguage.LanguageOne)
                {
                    MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].YearLan1 = DisplayText;
                }
                else if (curLanguage == CurrentEditLanguage.LanguageTwo)
                {
                    MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].YearLan2 = DisplayText;
                }
                else if (curLanguage == CurrentEditLanguage.LanguageThree)
                {
                    MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].YearLan3 = DisplayText;
                }
                else if (curLanguage == CurrentEditLanguage.LanguageFour)
                {
                    MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].YearLan4 = DisplayText;
                }
            }
            else if (curEventCreatorClass == EventCreatorClass.SubEvent)
            {
                if (MainContainer.subEventIndex < MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].ListDates.Count)
                {
                    if (curLanguage == CurrentEditLanguage.LanguageOne)
                    {
                        MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].
                            ListDates[MainContainer.subEventIndex].TitleLan1 = DisplayText;
                    }
                    else if (curLanguage == CurrentEditLanguage.LanguageTwo)
                    {
                        MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].
                          ListDates[MainContainer.subEventIndex].TitleLan2 = DisplayText;
                    }
                    else if (curLanguage == CurrentEditLanguage.LanguageThree)
                    {
                        MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].
                          ListDates[MainContainer.subEventIndex].TitleLan3 = DisplayText;
                    }
                    else if (curLanguage == CurrentEditLanguage.LanguageFour)
                    {
                        MainContainer.hweventJson.EventGroups[MainContainer.mainEventIndex].
                          ListDates[MainContainer.subEventIndex].TitleLan4 = DisplayText;
                    }
                }
            }
        }

        private void InitialCommonControl()
        {
            textBlock.TextTrimming = TextTrimming.CharacterEllipsis;
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            textBlock.FontSize = 20;
            textBlock.IsEnabled = false;
            textBlock.Margin = new Thickness(10);
            textBlock.Style = FindResource("TextBlockStyle") as Style;

            textBox.TextWrapping = TextWrapping.Wrap;
            textBox.VerticalAlignment = VerticalAlignment.Top;
            textBox.Width = 270;
            textBox.Height = 80;
            textBox.FontSize = 22;

            textBlock.FlowDirection = curFlowDir;
            textBox.FlowDirection = curFlowDir;
        }

        private void ControllerWithClickAndDoubleClick_Loaded(object sender, RoutedEventArgs e)
        {
            if (curControlMode == ControlMode.Display)
            {
                AddDisplayControl();
            }
            else if (curControlMode == ControlMode.Edit)
            {
                AddEditControl();
            }
            else if (curControlMode == ControlMode.Create)
            {
                CreateEditControl();
            }
        }

        private void AddDisplayControl()
        {
            uxMainControl.Children.Clear();
            textBlock.Text = DisplayText;
            this.Height = 70;
            CheckTextLength(textBlock.Text);

            uxMainControl.Children.Add(textBlock);
        }

        private void AddEditControl()
        {
            uxMainControl.Children.Clear();
            this.Height = 80;
            textBox.Text = DisplayText;

            uxMainControl.Children.Add(textBox);
        }

        private void CreateEditControl()
        {
            uxMainControl.Children.Clear();
            this.Height = 80;
            textBox.Text = defaultAddStr;

            uxMainControl.Children.Add(textBox);
        }

        public void LetGetFocus()
        {
            if (curControlMode == ControlMode.Edit || curControlMode == ControlMode.Create)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Input,
                                new Action(delegate ()
                                {
                                    textBox.Focus();
                                    Keyboard.Focus(textBox);
                                    textBox.SelectAll();
                                }));
            }
        }

        public void LetLostFocus()
        {
            if (curControlMode == ControlMode.Edit || curControlMode == ControlMode.Create)
            {
                //失去焦點將現有字串存起來
                if (textBox.Text != defaultAddStr)
                    DisplayText = textBox.Text;
                SaveDataToBuffer();
                AddDisplayControl();
            }
            curControlMode = ControlMode.Display;
        }

        #region 判斷Click or Double Click Function
        private void mouseWaitTimer_Tick(object sender, EventArgs e)
        {
            myClickWaitTimer.Stop();

            Grid_MouseClick();
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            IsMouseDown = true;
        }

        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (IsMouseDown)
            {
                if (!myClickWaitTimer.IsEnabled)
                {
                    myClickWaitTimer.Start();
                }
                else
                {
                    myClickWaitTimer.Stop();
                    Grid_DoubleClick();
                    e.Handled = true;
                }
            }
            IsMouseDown = false;
        }
        #endregion

        private int GetLegalLength()
        {
            int result = 10;
            if (curEventCreatorClass == EventCreatorClass.SubEvent)
            {
                return 30;
            }

            return result;
        }

        private void CheckTextLength(string curStr)
        {
            int legalLength = GetLegalLength();

            if (curStr.Length >= legalLength)
            {
                uxOverLengthSign.Opacity = 1;
            }
            else
            {
                uxOverLengthSign.Opacity = 0;
            }
        }

        private void Grid_MouseClick()
        {

        }

        private void Grid_DoubleClick()
        {
            curControlMode = ControlMode.Edit;
            AddEditControl();
            LetGetFocus();
        }
    }
}
