using System;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

using Unity;

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

        WdMainVm viewModel;
        NavigationService NavigationService => frame.NavigationService;

        public Action<IUnityContainer> Register;
        public MainWindow()
        {
            InitializeComponent();

            viewModel = new WdMainVm();
            this.DataContext = viewModel;//为了隐藏报警条

            container = new UnityContainer();
            Application.Current.Properties["container"] = container;
        }

        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.RegisterInstance(lp);
            Register(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);

            //加载主页面后 删除 PgLoading 页面
            page.Loaded += Page_Loaded;
            //---------------------------------------------------------
            //初始化
            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 Page_Loaded(object sender, RoutedEventArgs e)
        {
            var page = sender as Page;
            page.Loaded -= Page_Loaded;

            NavigationService.RemoveBackEntry();
        }

        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; }
    }
}
