using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DataBaseConnection.Control;

using DevExpress.XtraBars.Docking2010;
using DevExpress.XtraBars.Docking2010.Views;
using DevExpress.XtraBars.Docking2010.Views.Tabbed;

using SystemX.Product.ALIS.Interface;
using SystemX.Product.ALIS.UI.View.InfoList;

using static SystemX.Product.ALIS.UI.Commons;
using static SystemX.Product.ALIS.UI.View.ViewCfg;

namespace SystemX.Product.ALIS.UI.View
{
    public partial class FrmChild : Form, IChildSubscribe
    {
        /// <summary>
        /// IChildSubscribe - setLoginStateNotice
        /// </summary>
        /// <param name="CurrentAccessLevel"></param>
        /// <param name="bLoginState"></param>
        public void SetLoginStateNotice(LoginAccessLevel CurrentAccessLevel, bool bLoginState)
        {
            ((IUserControlSubscribe)WindowControl).SetLoginStateNotice(CurrentAccessLevel, bLoginState);
        }

        /// <summary>
        /// IChildSubscribe - GoToFindTestListFileFocus
        /// </summary>
        /// <param name="nTestListFileNo"></param>
        public void GoToFindTestListFileFocus(int nTestListFileNo)
        {
            ((IUserControlSubscribe)WindowControl).GoToFindTestListFileFocus(nTestListFileNo);
        }

        /// <summary>
        /// IChildSubscribe - GoToFindVariantFocus
        /// </summary>
        /// <param name="nParentNo"></param>
        public void GoToFindVariantFocus(int nVariantNo)
        {
            ((IUserControlSubscribe)WindowControl).GoToFindVariantFocus(nVariantNo);
        }

        public string DisplayName { get; private set; }
        public eAppFunctionType DocType { get; private set; }
        public UserControl WindowControl { get; private set; }

        private IChildController refChildInterface;
        private IDataController refDBInterface;

        public LoginAccessLevel ThisAccessLevel { get; private set; }

        public FrmChild(Form mainform, eAppFunctionType eType, eSelectType eSelType, string strDispModelName, LoginAccessLevel CreateAccessLevel, bool bLoginState = false)
        {
            InitializeComponent();

            ThisAccessLevel = CreateAccessLevel;

            refChildInterface = (IChildController)mainform;
            refDBInterface = (IDataController)mainform;

            DisplayName = strDispModelName;
            DocType = eType;

            WindowControl = CreateContents(eType, eSelType, ThisAccessLevel, bLoginState);
            WindowControl.Parent = mainform;

            panelControlDocFrame.Controls.Add(WindowControl);
            panelControlDocFrame.Dock = DockStyle.Fill;
            panelControlDocFrame.Name = strDispModelName;

            this.Name = strDispModelName;
            this.Dock = DockStyle.Fill;

            MdiParent = mainform;
        }
        UserControl CreateContents(eAppFunctionType eType, eSelectType eSelType, LoginAccessLevel MakeAccessLevel, bool bLoginState = false)
        {
            switch (eType)
            {
                case eAppFunctionType.M_HostList:
                case eAppFunctionType.M_UserList:
                case eAppFunctionType.M_GroupList:
                case eAppFunctionType.M_TestCodeList:
                    {
                        UcInfoView UcView = new UcInfoView(eSelType, refDBInterface, MakeAccessLevel, bLoginState) { Dock = DockStyle.Fill };
                        return UcView;
                    }
                case eAppFunctionType.M_TestListFile:
                case eAppFunctionType.M_TestListVariant:
                case eAppFunctionType.M_TestListRelease:
                    {
                        UcTestListView UcView = new UcTestListView(eSelType, refChildInterface, refDBInterface, MakeAccessLevel, bLoginState) { Dock = DockStyle.Fill };
                        return UcView;
                    }
                default:
                    return null;
            }
        }

        public void AddChildControl(BaseView parentView, FrmChild docFramesHide)
        {
            BaseDocumentCollection document = parentView.Documents;
            DevExpress.XtraBars.Docking2010.Views.Tabbed.Document baseDoc = document.Find(x => x.Control == this).FirstOrDefault() as DevExpress.XtraBars.Docking2010.Views.Tabbed.Document;
            DevExpress.XtraBars.Docking2010.Views.Tabbed.Document baseDocHiding = document.Find(x => x.Control == docFramesHide).FirstOrDefault() as DevExpress.XtraBars.Docking2010.Views.Tabbed.Document; ;

            DevExpress.XtraBars.Docking2010.DocumentManager ctrlMng = baseDoc.Parent.Manager;

            var index = baseDoc.Parent.Items.IndexOf(baseDoc);

            /*
            baseDocHiding.Parent.Items.Remove(baseDocHiding);
            baseDoc.Parent.Items.Insert(index, baseDocHiding);
            (ctrlMng.View as TabbedView).Controller.Select(baseDocHiding);
            */

            (parentView as TabbedView).Controller.Dock(baseDocHiding, baseDoc.Parent, index);
            (ctrlMng.View as TabbedView).Controller.Select(baseDoc);

            SetTabSpace(parentView as TabbedView, ctrlMng, baseDoc.Parent);
            Rebalance(ctrlMng, parentView as TabbedView);
        }

        private void Rebalance(DocumentManager docMng, TabbedView parentView)
        {
            docMng.BeginUpdate();
            foreach (var documentGroup in parentView.DocumentGroups)
            {
                documentGroup.GroupLength = 1;
                documentGroup.Visible = true;
            }
            parentView.AppearancePage.Header.Font = new Font(parentView.AppearancePage.Header.Font, FontStyle.Regular);
            docMng.EndUpdate();
        }

        private void SetTabSpace(TabbedView parentView, DocumentManager ctrlMng, DocumentGroup currentGroup)
        {
            int size = 200; //MtFormUserSettings.LoadScreenUserSettings(cnnInfo, mtSysMDAppMenuNodesIRN);

            // Will be zero if form was floated
            if (size == 0)
            {
                return;
            }

            if (size == 100)
            {
                MakeProminent(parentView, currentGroup);
                return;
            }

            ctrlMng.BeginUpdate();

            foreach (var documentGroup in parentView.DocumentGroups)
            {
                if (documentGroup == currentGroup)
                {
                    continue;
                }
                documentGroup.GroupLength = Convert.ToInt32(100 - size) / (parentView.DocumentGroups.Count - 1);
            }

            currentGroup.GroupLength = size;

            ctrlMng.EndUpdate();
        }

        private void MakeProminent(TabbedView parentView, DocumentGroup currentGroup)
        {
            foreach (var documentGroup in parentView.DocumentGroups)
            {
                if (documentGroup == currentGroup)
                {
                    continue;
                }
                documentGroup.Visible = false;
                //reset the parent doc group's groupLength to 0 since the child doc group is going 
                //to take up the whole screen
                documentGroup.GroupLength = 0;
            }
            parentView.AppearancePage.Header.Font = new Font(parentView.AppearancePage.Header.Font, FontStyle.Bold);
        }

        private void FrmChild_FormClosed(object sender, FormClosedEventArgs e)
        {
            refChildInterface.CloseChildForm(DocType);
        }

        private void FrmChild_Activated(object sender, EventArgs e)
        {
            refChildInterface.CurrentSelectFunctionType(DocType);
        }

        public void RefreshView()
        {
            ((IUserControlSubscribe)WindowControl).RefreshDataView();
        }
    }
}
