using AIStudio.Wpf.DiagramApp.Models;
using AIStudio.Wpf.DiagramApp.ViewModels;
using AIStudio.Wpf.Flowchart.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using AIStudio.Wpf.DiagramDesigner;
using AIStudio.Wpf.Mind.ViewModels;
using AIStudio.Wpf.Mind;
using AIStudio.Wpf.DiagramDesigner.Additionals;
using AIStudio.Wpf.Mind.Helpers;

namespace AIStudio.Wpf.Flowchart
{
    public class MindViewModel : PageViewModel
    {
        public MindViewModel(string title, string status, DiagramType diagramType, MindType mindType) : base(title, status, diagramType)
        {
            MindType = mindType;
        }
        public MindViewModel(string filename, DiagramDocument diagramDocument) : base(filename, diagramDocument)
        {
            foreach (var vm in DiagramViewModels)
            {
                vm.Init();
            }

            if (MindDiagramViewModel != null)
            {
                MindType = MindDiagramViewModel.MindType;
            }
        }

        protected override void InitDiagramViewModel()
        {
            base.InitDiagramViewModel();

            DiagramViewModel.GridCellSize = new Size(100, 100);
            DiagramViewModel.ShowGrid = false;
            DiagramViewModel.AllowDrop = false;
        }

        public IMindDiagramViewModel MindDiagramViewModel
        {
            get
            {
                return DiagramViewModel as IMindDiagramViewModel;
            }
        }

        private MindType _mindType = Mind.MindType.Mind;
        public MindType MindType
        {
            get
            {
                return _mindType;
            }
            set
            {
                if (SetProperty(ref _mindType, value))
                {
                    foreach (var vm in DiagramViewModels.OfType<IMindDiagramViewModel>())
                    {
                        vm.MindType = MindType; 
                    }
                }
            }
        }

        private MindThemeModel _mindThemeModel = MindThemeHelper.GetTheme("天空蓝");
        public MindThemeModel MindThemeModel
        {
            get
            {
                return _mindThemeModel;
            }
            set
            {
                if (SetProperty(ref _mindThemeModel, value))
                {
                    foreach (var vm in DiagramViewModels.OfType<IMindDiagramViewModel>())
                    {
                        vm.MindThemeModel = MindThemeHelper.GetTheme(MindThemeModel?.Name); 
                    }
                }
            }
        }

        protected override void Init()
        {
            DiagramViewModels = new ObservableCollection<IDiagramViewModel>()
            {
                GetDiagramViewModel("页-1", DiagramType),
            };
            DiagramViewModel = DiagramViewModels.FirstOrDefault();

            InitDiagramViewModel();
            var level1node = MindDiagramViewModel.RootItems.FirstOrDefault();

            MindNode level2node1_1 = new MindNode(DiagramViewModel) { Text = "分支主题1" };
            level2node1_1.AddTo(level1node, 0, false);

            MindNode level2node1_1_1 = new MindNode(DiagramViewModel) { Text = "分支主题1_1" };
            level2node1_1_1.AddTo(level2node1_1, 0, false);

            MindNode level2node1_1_2 = new MindNode(DiagramViewModel) { Text = "分支主题1_2" };
            level2node1_1_2.AddTo(level2node1_1, 0, false);         

            MindNode level2node1_1_3 = new MindNode(DiagramViewModel) { Text = "分支主题1_3" };
            level2node1_1_3.AddTo(level2node1_1, 0, false);         

            MindNode level2node1_2 = new MindNode(DiagramViewModel) { Text = "分支主题2" };
            level2node1_2.AddTo(level1node, 0, false); 

            MindNode level2node1_3 = new MindNode(DiagramViewModel) { Text = "分支主题3" };
            level2node1_3.AddTo(level1node, 0, false);

            DiagramViewModel.Init();
        }

        protected override DiagramViewModel GetDiagramViewModel(string name, DiagramType diagramType)
        {
            var viewmodel = new MindDiagramViewModel() { Name = name ?? NewNameHelper.GetNewName(DiagramViewModels.Select(p => p.Name), "页-"), DiagramType = diagramType, MindType = MindType, MindThemeModel = MindThemeHelper.GetTheme(MindThemeModel?.Name) };
            viewmodel.Init();

            return viewmodel;
        }

        public override void Dispose()
        {
            base.Dispose();

            foreach (var viewModel in DiagramViewModels)
            {
                FlowchartService.DisposeData(viewModel);
            }
        }
    }
}
