using MultiLayout;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Unity;
using UnityConfigurationTree;
using LoadingProgress = MultiLayout.LoadingProgress;

namespace MultiLayout
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        IUnityContainer container;
        LoadingProgress lp;
        FlyLayoutManager manager;

        WdMainVm viewModel;
        NavigationService NavigationService => frame.NavigationService;
        public MainWindow()
        {
            InitializeComponent();
            
            viewModel = new WdMainVm();
            this.DataContext = viewModel;//为了隐藏报警条
        }

        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //---------------------------------------------------------
            //转到进度条页面
            lp = new LoadingProgress();
            PgLoading p = new PgLoading();
            p.DataContext = lp;

            NavigationService.Navigate(p);

            await Task.Delay(1000);
            //---------------------------------------------------------
            //加载 unity
            container = new UnityContainer();
            Application.Current.Properties["container"] = container;
            container.RegisterInstance(lp);
            UnityConfExt.Load(container);

            container.RegisterInstance<Window>(this);

            //---------------------------------------------------------
            //加载主界面布局
            FlyLayout layout = new FlyLayout();
            layout.Load();

            //初始化
            manager = new FlyLayoutManager();
            container.RegisterInstance(manager);
            manager.Init(
                layout, container, lp);
            this.DataContext = manager;

            //以后的页面切换器
            FlyLayoutManager.NavigationService = NavigationService;

            //---------------------------------------------------------
            //ui 加载前初始化
            await manager.OnPreInit();

            //加载模块构造主界面,如果是多设备界面， FlyLayoutManager.NavigationService 会被修改为 page内部的 NavigationService
            var page = await manager.LoadModule();
            NavigationService.Navigate(page);

            page.Loaded += (s, e1) =>
                frame.NavigationService.RemoveBackEntry();

            //---------------------------------------------------------
            //初始化
            await manager.OnInit();


            //启动Poll系统
            //FObjBase.PollModule.Current.Start();

            ErrMsgJsonDb jsonDb = new ErrMsgJsonDb();
            if (jsonDb.Load()) {
                var m = btnErrMsg.Margin;
                m.Right = jsonDb.Right;
                btnErrMsg.Margin = m;
            }

        }

        private void btnErrMsgClick(object sender, RoutedEventArgs e)
        {
            manager.ErrMsgClick?.Invoke();
        }

        private void DrapMoveXBehavior_Move(object sender, EventArgs e)
        {
            ErrMsgJsonDb jsonDb = new ErrMsgJsonDb();
            jsonDb.Right = (int)btnErrMsg.Margin.Right;
            jsonDb.Save();
        }
    }
    public class ErrMsgJsonDb 
    {
        public int Right;
        private string filePath = "errMsgPosition.json";
        public bool Save() 
        {
            try
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(this);
                File.WriteAllText(filePath, json);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Load() 
        {
            if (!File.Exists(filePath))
                return false;
            try
            {
                string json = File.ReadAllText(filePath);
                Newtonsoft.Json.JsonConvert.PopulateObject(json, this);
                return true;
            }
            catch {
                return false;
            }
        }
    }
    public class WdMainVm:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public bool IsErrMsgVisable => !string.IsNullOrEmpty(ErrMsg);

        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrMsg { get; set; }
    }
}
