using Microsoft.Office.Interop.Word;
using PDF_Office.EventAggregators;
using PDF_Office.Model.EditTools.Background;
using PDF_Office.Properties;
using PDFSettings;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace PDF_Office.ViewModels.EditTools.Background
{

    public class BackgroundTemplateListColorContentViewModel : BindableBase, INavigationAware
    {
        private readonly IEventAggregator eventAggregator;
        public BackgroundInfo BackgroundInfo = new BackgroundInfo();
        public BackgroundItem BackgroundItem = new BackgroundItem();
        public int TemplateIndex = 0;

        private ObservableCollection<BackgroundItem> backgroundModcolorCollection = new ObservableCollection<BackgroundItem>();
        public ObservableCollection<BackgroundItem> BackgroundModColorCollection
        {
            get { return backgroundModcolorCollection; }
            set
            {
                backgroundModcolorCollection = value;
                RaisePropertyChanged();
            }
        }

        private Visibility _createTemplateVisible;
        public Visibility CreateTemplateVisible
        {
            get { return _createTemplateVisible; }
            set { SetProperty(ref _createTemplateVisible, value); }
        }

        public DelegateCommand AddTemplateCommand { get; set; }
        public DelegateCommand<object> DeleteTemplateItemCommand { get; set; }
        public DelegateCommand<object> EditTemplateItemCommand { get; set; }
        public DelegateCommand DeleteAllTemplateItemCommand { get; set; }
        public DelegateCommand<object> SelectTemplateItemCommand { get; set; }

        public string unicode = null;
        public string Unicode = null;

        public BackgroundTemplateListColorContentViewModel(IEventAggregator eventAggregator)
        {
            Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
            this.eventAggregator = eventAggregator;
            AddTemplateCommand = new DelegateCommand(AddTemplate);
            EditTemplateItemCommand = new DelegateCommand<object>(RequestEditTemplateItem);
            DeleteTemplateItemCommand = new DelegateCommand<object>(DeleteTemplateItem);
            DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem);
            SelectTemplateItemCommand = new DelegateCommand<object>(SelectTemplateItem);

            eventAggregator.GetEvent<SaveEditedBackgroundTemplateItemEvent>().Subscribe(SaveEditedBackgroundTemplateItem, e => e.Unicode == Unicode);
        }

        private void CheckTemplateListIsEmpty(List<BackgroundItem> backgroundTemplateList)
        {
            if (backgroundTemplateList.Count() == 0)
            {
                CreateTemplateVisible = Visibility.Visible;
            }
            else
            {
                CreateTemplateVisible = Visibility.Collapsed;
            }
        }

        private void GetBackgroundSource()
        {
            List<BackgroundItem> backgroundModColorTemplateList = new List<BackgroundItem>();
            for (int temp = 0; temp < Settings.Default.BackgroundTemplateList.Count; temp++)
            {
                if (Settings.Default.BackgroundTemplateList[temp].type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_COLOR)
                {
                    backgroundModColorTemplateList.Add(Settings.Default.BackgroundTemplateList[temp]);
                }
            }
            BackgroundModColorCollection = new ObservableCollection<BackgroundItem>(backgroundModColorTemplateList);
            CheckTemplateListIsEmpty(backgroundModColorTemplateList);
        }

        private void InitBackgroundTemplateList()
        {
            if (Settings.Default.BackgroundTemplateList == null)
            {
                Settings.Default.BackgroundTemplateList = new BackgroundTemplateList();
            }
            GetBackgroundSource();
        }

        public void AddTemplate()
        {
            this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
            {
                Unicode = Unicode,
                Status = EnumTemplateListOrCreate.StatusCreate
            });
        }

        public void SaveEditedBackgroundTemplateItem(BackgroundItemUnicode backgroundItemunicode)
        {
            BackgroundItem backgroundItem = backgroundItemunicode.Status;
            if (backgroundItem.type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_COLOR)
            {
                Settings.Default.BackgroundTemplateList[TemplateIndex] = backgroundItem;
                Settings.Default.Save();
            }
            GetBackgroundSource();
        }


        public void RequestEditTemplateItem(object e)
        {
            var control = e as Control;
            if (control == null)
            {
                return;
            }
            var template = control.DataContext as BackgroundItem;
            if (template == null)
            {
                return;
            }
            TemplateIndex = Settings.Default.BackgroundTemplateList.IndexOf(template);
            template.listIndex = TemplateIndex;
            ConvertItemToInfo(template, ref BackgroundInfo);
            this.eventAggregator.GetEvent<EditBackgroundTemplateItemEvent>().Publish(new BackgroundItemUnicode
            {
                Unicode = Unicode,
                Status = template
            });
        }


        public void DeleteTemplateItem(object e)
        {
            var btn = e as System.Windows.Controls.Button;
            if (btn == null)
            {
                return;
            }
            BackgroundItem = btn.DataContext as BackgroundItem;
            if (BackgroundItem == null)
            {
                return;
            }
            if (!string.IsNullOrEmpty(BackgroundItem.previewImagePath) && File.Exists(BackgroundItem.previewImagePath))
            {
                Settings.Default.AppProperties.NeedToDeletePath.Add(BackgroundItem.previewImagePath);
            }
            Settings.Default.BackgroundTemplateList.Remove(BackgroundItem);
            Settings.Default.Save();
            BackgroundModColorCollection.Remove(BackgroundItem);
            GetBackgroundSource();
        }

        public void DeleteAllTemplateItem()
        {
            while (Settings.Default.BackgroundTemplateList.Count != 0)
            {
                int temp = 0;
                var template = Settings.Default.BackgroundTemplateList[temp];
                if (template.type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE)
                {
                    if (!string.IsNullOrEmpty(BackgroundItem.previewImagePath) && File.Exists(BackgroundItem.previewImagePath))
                    {
                        Settings.Default.AppProperties.NeedToDeletePath.Add(BackgroundItem.previewImagePath);
                    }

                    Settings.Default.BackgroundTemplateList.Remove(template);
                    Settings.Default.Save();

                    BackgroundModColorCollection.Remove(template);
                }
            }
            GetBackgroundSource();
        }

        public void ConvertItemToInfo(BackgroundItem backgroundItem, ref BackgroundInfo backgroundInfo)
        {
            backgroundInfo.BackgroundType = backgroundItem.type;
            backgroundInfo.Color = backgroundItem.bgColor;
            backgroundInfo.Horizalign = backgroundItem.horizalign;
            backgroundInfo.Vertalign = backgroundItem.vertalign;
            backgroundInfo.VertOffset = backgroundItem.vertOffset;
            backgroundInfo.Horizalign = backgroundItem.horizalign;
            backgroundInfo.Opacity = backgroundItem.opacity;
            backgroundInfo.Rotation = backgroundItem.rotation;
            backgroundInfo.Scale = backgroundItem.scale;
            //  backgroundItem.pagRangeMode = backgroundInfo.PageRange;

        }

        public void SendTemplateItemToDocument(BackgroundItem template)
        {
            if (template != null)
            {
                ConvertItemToInfo(template, ref BackgroundInfo);
                eventAggregator.GetEvent<SetBackgroundEvent>().Publish(new BackgroundInfoUnicode
                {
                    Unicode = Unicode,
                    Status = BackgroundInfo
                });
            }

        }


        public void SelectTemplateItem(object e)
        {
            var listBox = e as ListBox;
            BackgroundItem BackgroundItem = listBox.SelectedItem as BackgroundItem;
            SendTemplateItemToDocument(BackgroundItem);
        }


        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {

            navigationContext.Parameters.TryGetValue<string>("UniCode", out unicode);
            InitBackgroundTemplateList();
        }
    }
}
