using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

using BeWo.Validation;

using BS.Shared;


namespace BeWo.View
{
    public class BeWoView : UserControl
    {
        public Action ModalEditViewUpdateCallback;

        public virtual bool IsDoneWithInsertOrUpdate { get; set; }

        public event EventHandler BlendOutComplete;

        public virtual bool CanDelete
        {
            get
            {
                return false;
            }
        }

        public virtual bool CanExcelExport
        {
            get
            {
                return false;
            }
        }

        public virtual bool CanWordExport
        {
            get
            {
                return false;
            }
        }

        public virtual bool IsDirty
        {
            get
            {
                return false;
            }
        }

        public virtual BeWoView ParentView { get; set; }

        public virtual string SaveObjectName
        {
            get
            {
                return this.Title;
            }
        }

        public virtual string Title
        {
            get
            {
                return string.Empty;
            }
        }

        public virtual bool IsInWindow { get; set; }

        internal virtual DependencyObject ValidationOnSaveRootElement
        {
            get
            {
                return this;
            }
        }

        protected MainControl MainControl
        {
            get
            {
                return BeWoApp.MainControl;
            }
        }

        public void BlendIn()
        {
            DoubleAnimation lAnim = new DoubleAnimation(1, new Duration(TimeSpan.FromSeconds(0.3)));
            this.BeginAnimation(OpacityProperty, lAnim);
        }

        public void BlendOut()
        {
            DoubleAnimation lAnim = new DoubleAnimation(0, new Duration(TimeSpan.FromSeconds(0.3)));
            lAnim.Completed += (s, e) =>
                                    {
                                        if (this.BlendOutComplete != null)
                                        {
                                            this.BlendOutComplete(this, e);
                                        }
                                    };
            this.BeginAnimation(OpacityProperty, lAnim);
        }

        public virtual void Delete()
        {
        }

        public virtual void ExportToExcel(string pFileID)
        {
        }

        public virtual void ExportToWord(string pFileID)
        {
        }

        public virtual void ModalPopUpCloseInvoked()
        {
        }

        public virtual void ModalPopUpSaveInvoked()
        {
        }

        public virtual void ChildViewClosed()
        {
        }

        public bool SaveData()
        {
            if (!BeWoApp.HasLoggedOnUserRight(this.GetSaveDemands()))
            {
                MessageBox.Show("Sie besitzen nicht die nötigen Rechte um zu speichern. Bitte wenden Sie sich an den Adminstrator!", "Autorisierung fehlgeschlagen", MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }

            var lValid = ValidationTrigger.Validate(this.ValidationOnSaveRootElement);
            if (lValid)
            {
                Save();
            }
            else
            {
                MessageBox.Show("Bitte füllen Sie alle Pflichtfelder aus!", "Speichern nicht möglich", MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            return lValid;
        }

        public virtual MessageBoxResult ShowSaveQuestion()
        {
            return string.IsNullOrEmpty(this.SaveObjectName) ? MessageBox.Show("Wollen Sie die Änderungen speichern?", "Änderungen speichern", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) : MessageBox.Show("Wollen Sie die Änderungen an " + this.SaveObjectName + " speichern?", "Änderungen speichern", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
        }

        internal virtual bool DoSaveCheck()
        {
            if (this.IsDirty)
            {
                MessageBoxResult lResult = this.ShowSaveQuestion();
                if (lResult == MessageBoxResult.Cancel)
                {
                    this.Focus();
                    return false;
                }

                if (lResult == MessageBoxResult.Yes)
                {
                    this.SaveData();
                }
            }

            return true;
        }

        protected virtual UserRightType[] GetSaveDemands()
        {
            return null;
        }


        protected virtual void Save()
        {
        }

        protected bool Validate(DependencyObject lElement)
        {
            return ValidationTrigger.Validate(lElement);
        }

        protected virtual void popup_Closed(object sender, EventArgs e)
        {
            this.MainControl.AnimateOpacity(1, 100);
        }

        protected virtual void popup_Opened(object sender, EventArgs e)
        {
            this.MainControl.AnimateOpacity(0.5, 200);
        }
    }
}