using MultiLayout;
using System;
using System.Collections.Generic;
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 : NavigationWindow
    {
        IUnityContainer container;
        LoadingProgress lp;
        FlyLayoutManager manager;

        public MainWindow()
        {
            InitializeComponent();

        }

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

            FlyLayoutManager.NavigationService = NavigationService;

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

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

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

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


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