using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Diagnostics;
using Sched.CustomClasses;
using Sched.CustomDialogs;

namespace Sched.CustomControls
{
    public class LessonBox : Border
    {
        private Lesson _lssn;
        public Lesson Lssn
        {
            get { return _lssn; }
            set
            {
                _lssn = value;
                UpdateInfoText();
            }

        }
        public TextBlock txtB = new TextBlock();

        public static RoutedEvent LessonBoxDeleteEvent = EventManager.RegisterRoutedEvent("LessonBoxDelete", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(LessonBox));
        public event RoutedEventHandler LessonBoxDelete
        {
            add { AddHandler(LessonBoxDeleteEvent, value); }
            remove { RemoveHandler(LessonBoxDeleteEvent, value); }
        }

        public static RoutedEvent LessonBoxEditEvent = EventManager.RegisterRoutedEvent("LessonBoxEdit", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(LessonBox));
        public event RoutedEventHandler LessonBoxEdit
        {
            add { AddHandler(LessonBoxEditEvent, value); }
            remove { RemoveHandler(LessonBoxEditEvent, value); }
        }

        public static RoutedEvent LessonBoxCopyEvent = EventManager.RegisterRoutedEvent("LessonBoxCopy", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(LessonBox));
        public event RoutedEventHandler LessonBoxCopy
        {
            add { AddHandler(LessonBoxCopyEvent, value); }
            remove { RemoveHandler(LessonBoxCopyEvent, value); }
        }

        public LessonBox()
        {
            //Border
            BorderBrush = Brushes.Black;
            BorderThickness = new Thickness(2);
            Margin = new Thickness(2,4,2,4);
            this.Child = txtB;

            txtB.TextAlignment = TextAlignment.Center;
            txtB.TextTrimming = TextTrimming.CharacterEllipsis;

            _lssn = new Lesson();
            UpdateInfoText();
            CreateContextMenu();
        }
        
        public void UpdateInfoText()
        {
            string infoStr = Lssn.GetInfoString();
            txtB.Text = TranslateInfoText(infoStr);

            string color = DBI.SelectReturnSingleValue("LessonType", "ColorHexcode", Lssn.LessonTypeId);
            if (color.Length != 7)
            {
                color = "#ff0000";
            }
            txtB.Background = new BrushConverter().ConvertFromString(color) as Brush;
        }

        private void CreateContextMenu()
        {
            ContextMenu = new ContextMenu();

            MenuItem delItem = new MenuItem();
            //delItem.Header = "Delete";
            delItem.SetResourceReference(MenuItem.HeaderProperty, "LessonBox_ContextMenu_Delete");
            delItem.Click += ContextMenu_delItem_Click;
            ContextMenu.Items.Add(delItem);

            MenuItem editItem = new MenuItem();
            //editItem.Header = "Edit";
            editItem.SetResourceReference(MenuItem.HeaderProperty, "LessonBox_ContextMenu_Edit");
            editItem.Click += ContextMenu_editItem_Click;
            ContextMenu.Items.Add(editItem);

            MenuItem copyItem = new MenuItem();
            //copyItem.Header = "Copy values to assignment";
            copyItem.SetResourceReference(MenuItem.HeaderProperty, "LessonBox_ContextMenu_Copy");
            copyItem.Click += ContextMenu_copyItem_Click;
            ContextMenu.Items.Add(copyItem);
        }

        private void ContextMenu_delItem_Click(object sender, RoutedEventArgs e)
        {
            string msg = (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["LessonBox_ContextMenu_Delete_Msg"];
            if (MessageBox.Show(msg, " ", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                RaiseEvent(new RoutedEventArgs(LessonBoxDeleteEvent, this));
            }
        }
        private void ContextMenu_editItem_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(LessonBoxEditEvent, this));
        }
        private void ContextMenu_copyItem_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(LessonBoxCopyEvent, this));
        }
    
        static public string TranslateInfoText(string infoText)
        {
            infoText = infoText.Replace("Str_Even", (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["Str_Even"]);
            infoText = infoText.Replace("Str_Odd", (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["Str_Odd"]);
            infoText = infoText.Replace("Str_StartDateDT", (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["Str_StartDateDT"]);
            infoText = infoText.Replace("Str_PeriodStart", (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["Str_PeriodStart"]);
            infoText = infoText.Replace("Str_Periodicity", (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["Str_Periodicity"]);
            return infoText;
        }
    
        public string GetLessonInfo()
        {
            return TranslateInfoText(Lssn.GetInfoString());
        }
        public string GetLessonInfoExtended()
        {
            return TranslateInfoText(Lssn.GetInfoStringExtended());
        }
    }
}
