using PDFSettings;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System.Windows;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using PDF_Office.Properties;
using Prism.Events;
using PDF_Office.EventAggregators;
using PDF_Office.Model.EditTools.Watermark;
using System.Windows.Controls;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.CustomControl;
using PDF_Office.Model;
using PDF_Office.Helper;
using PDF_Office.Model.EditTools.Background;
using PDF_Office.ViewModels.Tools;

namespace PDF_Office.ViewModels.EditTools.Watermark
{
    public class WatermarkTemplateListTextContentViewModel : BindableBase, INavigationAware
    {
        private readonly IEventAggregator eventAggregator;

        private CPDFViewer CurrentPDFViewer;

        public WatermarkInfo WatermarkInfo = new WatermarkInfo();

        public WatermarkItem WatermarkItem = new WatermarkItem();

        public int TemplateIndex = 0;

        public ObservableCollection<WatermarkItem> watermarkModTextCollection = new ObservableCollection<WatermarkItem>();
        public ObservableCollection<WatermarkItem> WatermarkModTextCollection
        {
            get { return watermarkModTextCollection; }
            set
            {
                watermarkModTextCollection = 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 WatermarkTemplateListTextContentViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
            AddTemplateCommand = new DelegateCommand(AddTemplate);
            DeleteTemplateItemCommand = new DelegateCommand<object>(DeleteTemplateItem);
            DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem);
            EditTemplateItemCommand = new DelegateCommand<object>(RequestEditTemplateItem);
            SelectTemplateItemCommand = new DelegateCommand<object>(SelectTemplateItem);
            eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Subscribe(CurrentWatermarkPDFViewer, e => e.Unicode == Unicode);
            eventAggregator.GetEvent<SaveEditedWatermarkTemplateItemEvent>().Subscribe(SaveEditedWatermarkTemplateItem, e => e.Unicode == Unicode);

        }
        public void CurrentWatermarkPDFViewer(CPDFViewerUnicode cPDFViewerunicode)
        {
            CurrentPDFViewer = cPDFViewerunicode.Status;
        }

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

        private void GetWatermarkSource()
        {
            List<WatermarkItem> watermarkModTextTemplateList = new List<WatermarkItem>();
            for (int temp = 0; temp < Settings.Default.WatermarkTemplateList.Count; temp++)
            {
                if (Settings.Default.WatermarkTemplateList[temp].type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_TEXT)
                {


                    watermarkModTextTemplateList.Add(Settings.Default.WatermarkTemplateList[temp]);
                }
            }
            WatermarkModTextCollection = new ObservableCollection<WatermarkItem>(watermarkModTextTemplateList);
            CheckTemplateListIsEmpty(watermarkModTextTemplateList);
        }



        private void InitWatermarkTemplateList()
        {
            if (Settings.Default.WatermarkTemplateList == null)
            {
                Settings.Default.WatermarkTemplateList = new WatermarkTemplateList();
            }
            GetWatermarkSource();
        }

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

        public void DeleteTemplateItem(object e)
        {
            var btn = e as System.Windows.Controls.Control;
            if (btn == null)
            {
                return;
            }
            var template = btn.DataContext as WatermarkItem;
            if (template == null)
            {
                return;
            }

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

            WatermarkModTextCollection.Remove(template);
            GetWatermarkSource();

        }

        public void DeleteAllTemplateItem()
        {
            while (Settings.Default.WatermarkTemplateList.Count != 0)
            {
                int temp = 0;
                var template = Settings.Default.WatermarkTemplateList[temp];
                if (template.type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_IMG)
                {
                    Settings.Default.WatermarkTemplateList.Remove(template);
                    Settings.Default.Save();
                    WatermarkModTextCollection.Remove(template);
                }
            }
            GetWatermarkSource();
        }

        public void RequestEditTemplateItem(object e)
        {
            var control = e as Control;
            if (control == null)
            {
                return;
            }
            var template = control.DataContext as WatermarkItem;
            if (template == null)
            {
                return;
            }
            TemplateIndex = Settings.Default.WatermarkTemplateList.IndexOf(template);
            template.listIndex = TemplateIndex;
            this.eventAggregator.GetEvent<EditWatermarkTemplateItemEvent>().Publish(new WatermarkItemUnicode
            {
                Unicode = Unicode,
                Status = template
            });
        }

        public void SelectTemplateItem(object e)
        {
            AlertsMessage alertsMessage = new AlertsMessage();
            if (CurrentPDFViewer.Document.GetWatermarkCount() > 0)
            {
                alertsMessage.ShowDialog("文件中已添加水印，您要替换现有的水印吗？", "", "取消", "确定");
                if (alertsMessage.result == ContentResult.Ok)
                {
                    this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
                    {
                        Unicode = Unicode,
                        Status = EnumDelete.StatusDeleteInitial
                    });
                }
                else
                {
                    this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
                    {
                        Unicode = Unicode,
                        Status = EnumDelete.StatusCreate
                    });
                }
            }
            var listBox = e as ListBox;
            WatermarkItem WatermarkItem = listBox.SelectedItem as WatermarkItem;
            SendTemplateItemToDocument(WatermarkItem);
        }

        public void SaveEditedWatermarkTemplateItem(WatermarkItemUnicode watermarkItemunicode)
        {
            WatermarkItem watermarkItem = watermarkItemunicode.Status;
            Settings.Default.WatermarkTemplateList[TemplateIndex] = watermarkItem;
            Settings.Default.Save();
            GetWatermarkSource();
        }

        public void ConvertItemToInfo(WatermarkItem watermarkItem, ref WatermarkInfo watermarkInfo)
        {
            if (watermarkItem != null)
            {
                watermarkInfo.WatermarkType = watermarkItem.type;
                watermarkInfo.Text = watermarkItem.text;
                watermarkInfo.TextSize = watermarkItem.fontSize;
                watermarkInfo.FontName = watermarkItem.fontName;
                watermarkInfo.TextColor = watermarkItem.textcolor;
                watermarkInfo.WatermarkHorizalign = watermarkItem.horizalign;
                watermarkInfo.WatermarkVertalign = watermarkItem.vertalign;
                watermarkInfo.Isfull = watermarkItem.isFullScreen;
                watermarkInfo.IsFront = watermarkItem.isfront;
                watermarkInfo.Opacity = watermarkItem.opacity;
                watermarkInfo.Rotation = watermarkItem.rotation;
                watermarkInfo.VertOffset = watermarkItem.vertOffset;
                watermarkInfo.HorizOffset = watermarkItem.horizOffset;
                watermarkInfo.VerticalSpacing = watermarkItem.verticalSpacing;
                watermarkInfo.HorizontalSpacing = watermarkItem.horizontalSpacing;
                EditToolsHelper.GetPageRange(watermarkItem.pageRangeIndex, CurrentPDFViewer.Document, ref WatermarkInfo.PageRange, watermarkItem.pageRange);
            }

        }

        public void SendTemplateItemToDocument(WatermarkItem WatermarkItem)
        {
            ConvertItemToInfo(WatermarkItem, ref WatermarkInfo);
            eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
            {
                Unicode = Unicode,
                Status = WatermarkInfo
            });
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            InitWatermarkTemplateList();
        }
    }
}
