using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemX.Net.Platform.Common.ExtensionMethods;
using SystemX.Product.ALIS.Interface;
using SystemX.Product.ALIS.UI.View;

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

namespace SystemX.Product.ALIS.UI
{
    public partial class FrmMain
    {
        public bool LoginCheck(LoginAccessLevel levelCheck)
        {
            if (Commons.DEBUG_MODE)
                return true;
            if (ACCESS_LEVEL == LoginAccessLevel.None)
                return false;

            switch (levelCheck)
            {
                case LoginAccessLevel.Admin:
                    return (ACCESS_LEVEL == LoginAccessLevel.Admin) ? true : false;
                case LoginAccessLevel.Basic:
                    return ((ACCESS_LEVEL & LoginAccessLevel.Basic) == LoginAccessLevel.Basic) ? true : false;
                default:
                    return false;
            }
        }

        private class OnOperationUserLoginMarker : IDisposable
        {
            private FrmMain _mainView;

            public OnOperationUserLoginMarker(FrmMain mv)
            {
                _mainView = mv;

                _mainView.Do(() =>
                {
                    _mainView.barButtonItem_Login.Enabled = false;
                });
            }

            public void Dispose()
            {
                _mainView.Do(() =>
                {
                    _mainView.barButtonItem_Login.Enabled = true;
                });
            }
        }

        private class OnOperationDBConnMarker : IDisposable
        {
            private FrmMain _mainView;

            private bool bConnBehavior = true;

            public OnOperationDBConnMarker(FrmMain mv, bool bSetConnBehavior = true)
            {
                _mainView = mv;

                bConnBehavior = bSetConnBehavior;

                _mainView.Do(() =>
                {
                    if (bConnBehavior)
                    {
                        _mainView.barButtonItemConnect.Enabled = false;
                        _mainView.barButtonItemLogout.Enabled = false;
                    }
                    else
                    {
                        _mainView.barButtonItemConnect.Enabled = false;
                        _mainView.barButtonItemLogout.Enabled = false;

                        _mainView.barStaticItemConn.Caption = "ConnectDB";
                        _mainView.barStaticItemConn.Visibility = BarItemVisibility.Never;
                        _mainView.barStaticItemConn.ShowImageInToolbar = false;
                    }
                });
            }

            public void Dispose()
            {
                _mainView.Do(() =>
                {
                    if (bConnBehavior)
                    {
                    }
                    else
                    {
                        _mainView.barButtonItemConnect.Enabled = true;
                        _mainView.barButtonItemLogout.Enabled = false;
                    }
                });
            }
        }

        public IDisposable GetOnOperationUserLoginMarker() { return new OnOperationUserLoginMarker(this); }

        public IDisposable GetOnOperationDBConnMarker(bool bSetConnBehavior = true) { return new OnOperationDBConnMarker(this, bSetConnBehavior); }


        public void DispUserInfoText(bool bSetInfo)
        {
            if (bSetInfo)
            {
                string strSetUserInfolb = CurrentUser.UserID + "|" +
                    CurrentUser.Name + "|" +
                    CurrentUser.Dept + "|" +
                    CurrentUser.Email + "|" +
                    CurrentUser.Comment;

                if (barStaticLabelLogin.Caption.IndexOf(strSetUserInfolb) < 0)
                    barStaticLabelLogin.Caption = strSetUserInfolb;

                if (this.Text.IndexOf(strTitleLabel + " @ " + strSetUserInfolb) < 0)
                    this.Text = strTitleLabel + " @ " + strSetUserInfolb;
            }
            else
            {
                this.Text = strTitleLabel;

                barStaticLabelLogin.Caption = "-";

                if (barStaticItemLoginTime1.Caption.IndexOf("-") < 0)
                    barStaticItemLoginTime1.Caption = "-";
                if (barStaticItemLoginTime2.Caption.IndexOf("-") < 0)
                    barStaticItemLoginTime2.Caption = "-";
            }
        }

        public void DispLoginTimeText()
        {
            if (barStaticItemLoginTime1.Caption.IndexOf(stCheckLoginTime.Elapsed.ToString("mm\\:ss")) < 0) //\\.ff")) < 0)
                barStaticItemLoginTime1.Caption = stCheckLoginTime.Elapsed.ToString("mm\\:ss"); //\\.ff");
            if (barStaticItemLoginTime2.Caption.IndexOf(stCheckLoginTime.Elapsed.ToString("mm\\:ss")) < 0) //\\.ff")) < 0)
                barStaticItemLoginTime2.Caption = stCheckLoginTime.Elapsed.ToString("mm\\:ss"); //\\.ff");
        }

        public void SetOnOperationMarker(bool bLoginState)
        {
            if (bLoginState)
            {
                barButtonItem_Login.Caption = "Login(F1)\r\n" + CurrentUser.UserID;
                barButtonItemUserLogout.Enabled = true;

                stCheckLoginTime.Restart();

                DispUserInfoText(true);
            }
            else
            {
                ACCESS_LEVEL = LoginAccessLevel.None;

                barButtonItem_Login.Caption = "Login(F1)";

                barButtonItemUserLogout.Enabled = false;

                DispUserInfoText(false);
            }

            //Child Notice loginState
            if (DocFrames != null)
            {
                foreach (FrmChild fc in DocFrames)
                    ((IChildSubscribe)fc).SetLoginStateNotice(ACCESS_LEVEL, bLoginState);
            }
        }
    }
}
