using PDF_Office.EventAggregators;
using PDF_Office.Model.EditTools.Background;
using PDF_Office.Model.EditTools.Watermark;
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.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace PDF_Office.ViewModels.EditTools.Background
{
    public class BackgroundTemplateListFileContentViewModel : BindableBase, INavigationAware
    {
        private readonly IEventAggregator eventAggregator;
        public BackgroundInfo BackgroundInfo = new BackgroundInfo();
        public BackgroundItem BackgroundItem = new BackgroundItem();
        public int TemplateIndex = 0;

        public ObservableCollection<BackgroundItem> backgroundModFileCollection = new ObservableCollection<BackgroundItem>();
        public ObservableCollection<BackgroundItem> BackgroundModFileCollection
        {
            get { return backgroundModFileCollection; }
            set
            {
                backgroundModFileCollection = 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 BackgroundTemplateListFileContentViewModel(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> backgroundModFileTemplateList = 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_IMAGE)
                {
                    backgroundModFileTemplateList.Add(Settings.Default.BackgroundTemplateList[temp]);
                }
            }
            BackgroundModFileCollection = new ObservableCollection<BackgroundItem>(backgroundModFileTemplateList);
            CheckTemplateListIsEmpty(backgroundModFileTemplateList);
        }



        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_IMAGE)
            {
                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;
            this.eventAggregator.GetEvent<EditBackgroundTemplateItemEvent>().Publish(new BackgroundItemUnicode
            {
                Unicode = Unicode,
                Status = template
            });
        }

        public void DeleteTemplateItem(object e)
        {
            var control = e as Control;
            if (control == null)
            {
                return;
            }
            var template = control.DataContext as BackgroundItem;
            if (template == null)
            {
                return;
            }
            if (!string.IsNullOrEmpty(BackgroundItem.previewImagePath) && File.Exists(BackgroundItem.previewImagePath))
            {
                Settings.Default.AppProperties.NeedToDeletePath.Add(BackgroundItem.previewImagePath);
            }

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

            BackgroundModFileCollection.Remove(template);
            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_COLOR)
                {

                    if (!string.IsNullOrEmpty(BackgroundItem.previewImagePath))
                    {
                        Settings.Default.AppProperties.NeedToDeletePath.Add(BackgroundItem.previewImagePath);
                    }

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

                    BackgroundModFileCollection.Remove(template);
                }
            }

            GetBackgroundSource();
        }

        public void ConvertItemToInfo(BackgroundItem backgroundItem, ref BackgroundInfo backgroundInfo)
        {
            if (backgroundItem != null)
            {
                backgroundInfo.BackgroundType = backgroundItem.type;
                backgroundInfo.ImageArray = backgroundItem.imageArray;
                backgroundInfo.ImageWidth = backgroundItem.imageWidth;
                backgroundInfo.ImageHeight = backgroundItem.imageHeight;
                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 BackgroundItem)
        {
            ConvertItemToInfo(BackgroundItem, 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)
        {
            InitBackgroundTemplateList();
        }
    }
}
