using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using VIZ.Framework.Core;
using VIZ.Package.Domain;
using VIZ.Package.Plugin;
using VIZ.Package.Storage;

namespace VIZ.Package.Module
{
    /// <summary>
    /// 页添加视图模型
    /// </summary>
    public class PageAddViewModel : ViewModelBase
    {
        public PageAddViewModel()
        {
            // 初始化命令
            this.initCommand();
        }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void initCommand()
        {
            this.LoadedCommand = new VCommand(this.Loaded);
            this.EnterCommand = new VCommand(this.Enter);
            this.CancelCommand = new VCommand(this.Cancel);
        }

        // ===========================================================
        // Property
        // ===========================================================

        #region Scene -- 场景

        private string scene;
        /// <summary>
        /// 场景
        /// </summary>
        public string Scene
        {
            get { return scene; }
            set { scene = value; this.RaisePropertyChanged(nameof(Scene)); }
        }

        #endregion

        #region Remark -- 备注

        private string remark;
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark
        {
            get { return remark; }
            set { remark = value; this.RaisePropertyChanged(nameof(Remark)); }
        }

        #endregion

        #region Layer -- 层

        private VizLayer layer;
        /// <summary>
        /// 层
        /// </summary>
        public VizLayer Layer
        {
            get { return layer; }
            set { layer = value; this.RaisePropertyChanged(nameof(Layer)); }
        }

        #endregion

        #region TemplatePlugins -- 模板插件集合

        private List<PluginInfo> templatePlugins;
        /// <summary>
        /// 模板插件集合
        /// </summary>
        public List<PluginInfo> TemplatePlugins
        {
            get { return templatePlugins; }
            set { templatePlugins = value; this.RaisePropertyChanged(nameof(TemplatePlugins)); }
        }

        #endregion

        #region SelectedTemplatePlugin -- 选中的模板插件

        private PluginInfo selectedTemplatePlugin;
        /// <summary>
        /// 选中的模板插件
        /// </summary>
        public PluginInfo SelectedTemplatePlugin
        {
            get { return selectedTemplatePlugin; }
            set { selectedTemplatePlugin = value; this.RaisePropertyChanged(nameof(SelectedTemplatePlugin)); }
        }

        #endregion

        // ===========================================================
        // Command
        // ===========================================================

        #region LoadedCommand -- 加载命令

        /// <summary>
        /// 加载命令
        /// </summary>
        public VCommand LoadedCommand { get; set; }

        /// <summary>
        /// 加载
        /// </summary>
        private void Loaded()
        {
            // 模板插件集合
            var list = ApplicationDomainEx.PluginInfos.Where(
                           p => !string.IsNullOrWhiteSpace(p.Group) &&
                                p.Group != ApplicationConstants.APPLICATION_GROUP_NAME &&
                                p.Group == ApplicationDomainEx.VizConfig.PluginGroup &&
                                p.PluginType == PluginType.Page).ToList();

            list.Insert(0, PluginInfo.NONE);
            this.TemplatePlugins = list;
            // 选择默认模板
            PluginMapping mapping = ApplicationDomainEx.PluginMappingConfig?.Mappings?.FirstOrDefault(p => p.Scene == this.Scene);
            if (mapping != null)
            {
                this.SelectedTemplatePlugin = this.TemplatePlugins.FirstOrDefault(p => p != PluginInfo.NONE && p.ID == mapping.PluginID);
            }
        }

        #endregion

        #region EnterCommand -- 确定命令

        /// <summary>
        /// 确定命令
        /// </summary>
        public VCommand EnterCommand { get; set; }

        /// <summary>
        /// 确定
        /// </summary>
        private void Enter()
        {
            Window window = this.GetWindow();
            if (window == null)
                return;

            window.DialogResult = true;

            window.Close();
        }

        #endregion

        #region CancelCommand -- 取消命令

        /// <summary>
        /// 取消命令
        /// </summary>
        public VCommand CancelCommand { get; set; }

        /// <summary>
        /// 取消
        /// </summary>
        private void Cancel()
        {
            Window window = this.GetWindow();
            if (window == null)
                return;

            window.DialogResult = false;

            window.Close();
        }

        #endregion
    }
}