using Ease.Core.Model;



using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;

namespace HRM.BO
{
    public class WFSetupRule : BasicBaseObject
    {
        #region Constructor & Private Declarations
        public WFSetupRule()
        {
            _wfSetupId = 0;
            _stAuthorityLevel = 0;
            _priority = 0;
            _wfRuleType = EnumWFRuleType.FixedOGNode;
            _levelDepth = 0;
            _reqAppAll = false;
            _autoNotificationNode = 0;
            _finalDestinationNode = 0;
            _bIsRAuthority = false;
        }
        #endregion

        #region Properties

        #region WfSetupId : ID

        private int _wfSetupId;
        public int WFSetupId
        {
            get { return _wfSetupId; }
            set { _wfSetupId = value; }
        }

        #endregion

        #region StAuthorityLevel : int

        private int _stAuthorityLevel;
        public int StAuthorityLevel
        {
            get { return _stAuthorityLevel; }
            set { _stAuthorityLevel = value; }
        }

        #endregion

        #region Priority : int

        private int _priority;
        public int Priority
        {
            get { return _priority; }
            set { _priority = value; }
        }

        #endregion

        #region Property WFRuleType : EnumWFRuleType

        private EnumWFRuleType _wfRuleType;
        public EnumWFRuleType WFRuleType
        {
            get { return _wfRuleType; }
            set { _wfRuleType = value; }
        }

        #endregion

        #region LevelDepth : int

        private int _levelDepth;
        public int LevelDepth
        {
            get { return _levelDepth; }
            set { _levelDepth = value; }
        }

        #endregion

        #region ReqAppAll : Boolean

        private bool _reqAppAll;
        public bool ReqAppAll
        {
            get { return _reqAppAll; }
            set { _reqAppAll = value; }
        }


        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }


        #endregion

        #region AutoNotificationNode : ID

        private int _autoNotificationNode;
        public int AutoNotificationNode
        {
            get { return _autoNotificationNode; }
            set { _autoNotificationNode = value; }
        }

        #endregion

        #region FinalDestinationNode : int

        private int _finalDestinationNode;
        public int FinalDestinationNode
        {
            get { return _finalDestinationNode; }
            set { _finalDestinationNode = value; }
        }

        #endregion

        #region IsRAuthority : Boolean
        private bool _bIsRAuthority;
        public bool IsRAuthority
        {
            get { return _bIsRAuthority; }
            set { _bIsRAuthority = value; }
        }
        #endregion

        public List<WFRuleDetailDesignation> WFRuleDeatilDesignations { get; set; }
        public List<WFRuleDetailOS> WFRuleDeatilOSs { get; set; }
        public List<WFRuleDetailNode> WFRuleDetailNodes { get; set; }
        public List<WFRuleDetailManual> WFRuleDetailManual { get; set; }

        //#region WFRuleDeatilDesignations
        //private List<WFRuleDetailDesignation> _wFRuleDeatilDesignations;
        //public List<WFRuleDetailDesignation> WFRuleDeatilDesignations
        //{

        //    get
        //    {
        //        if (_wFRuleDeatilDesignations == null && this.int != null && this.ID.IsUnassigned == false)
        //            _wFRuleDeatilDesignations = WFSetup.Get4WFRuleDD(this.ID);
        //        return _wFRuleDeatilDesignations;
        //    }
        //    set { _wFRuleDeatilDesignations = value; }
        //}
        //#endregion

        //#region WFRuleDeatilOSs
        //private List<WFRuleDetailOS> _wFRuleDeatilOSs;
        //public List<WFRuleDetailOS> WFRuleDeatilOSs
        //{
        //    get
        //    {
        //        if (_wFRuleDeatilOSs == null) _wFRuleDeatilOSs = new List<WFRuleDetailOS>();
        //        return _wFRuleDeatilOSs;
        //    }
        //    set { _wFRuleDeatilOSs = value; }
        //}
        //#endregion

        //#region WFRuleDetailNodes
        //private List<WFRuleDetailNode> _wFRuleDetailNodes;
        //public List<WFRuleDetailNode> WFRuleDetailNodes
        //{
        //    get
        //    {
        //        if (_wFRuleDetailNodes == null && this.int != null && this.ID.IsUnassigned == false)
        //            _wFRuleDetailNodes = WFSetup.Get4WFRuleNode(this.ID);
        //        return _wFRuleDetailNodes;
        //    }
        //    set { _wFRuleDetailNodes = value; }
        //}
        //#endregion


        //#region WFRuleDetailNodes
        //private List<WFRuleDetailManual> _wFRuleDetailManual;
        //public List<WFRuleDetailManual> WFRuleDetailManual
        //{
        //    get
        //    {
        //        if (_wFRuleDetailManual == null && this.int != null && this.ID.IsUnassigned == false)
        //            _wFRuleDetailManual = WFSetup.Get4WFRuleManual(this.ID);
        //        return _wFRuleDetailManual;
        //    }
        //    set { _wFRuleDetailManual = value; }
        //}
        //#endregion


        #endregion

        //#region Functions
        //public bool IsNextTier(int nNextTier)
        //{
        //    switch (this.WFRuleType)
        //    {
        //        case EnumWFRuleType.StraightAuthority:
        //            break;
        //        case EnumWFRuleType.FixedOGNode:
        //            foreach (WFRuleDetailNode oItem in this.WFRuleDetailNodes)
        //            {
        //                if (oItem.SequenceId == nNextTier)
        //                {
        //                    return true;
        //                }
        //            }
        //            return false;
        //            break;
        //        case EnumWFRuleType.Designation:
        //            foreach (WFRuleDetailDesignation oItem in this.WFRuleDeatilDesignations)
        //                if (oItem.SequenceId == nNextTier)
        //                    return true;
        //            break;
        //        case EnumWFRuleType.OrganizationStructure:
        //            foreach (WFRuleDetailOS oItem in this.WFRuleDeatilOSs)
        //                if (oItem.SequenceId == nNextTier)
        //                    return true;

        //            break;
        //        case EnumWFRuleType.Manual:
        //            foreach (WFRuleDetailManual oItem in this.WFRuleDetailManual)
        //                if (oItem.SequenceId == nNextTier)
        //                    return true;

        //            break;

        //        default:
        //            break;
        //    }
        //    return false;
        //}

        //public bool HasNextApprovar(OrganogramBasic Node, int nTier)
        //{
        //    // following block is Only to capture IDLC Exception 

        //    //if (WFRuleType == EnumWFRuleType.StraightAuthority && this.WFSetupId.Integer == 1)
        //    //{
        //    //    if (nTier == 3)
        //    //    {
        //    //        if (Node.Parent == null) return false;
        //    //        if (Node.Parent.Parent == null) return false;
        //    //    }
        //    //}

        //    bool bNext = false;
        //    if (nTier <= this.LevelDepth)
        //    {
        //        bNext = true;
        //    }
        //    return bNext;
        //}
        //public bool xHasNextApprovar(int nTier)
        //{

        //    bool bNext = false;
        //    if (nTier <= this.LevelDepth)
        //    {
        //        bNext = true;
        //    }
        //    return bNext;
        //}

        //public List<WFRuleDetailManual> RemoveaSequence(List<WFRuleDetailManual> oRuleDetailManual, int nSquenceID)
        //{
        //    List<WFRuleDetailManual> truleDetailmn = new List<WFRuleDetailManual>();
        //    foreach (WFRuleDetailManual oItem in oRuleDetailManual)
        //    {
        //        if (oItem.SequenceId != nSquenceID)
        //        {
        //            truleDetailmn.Add(oItem);
        //        }
        //    }

        //    oRuleDetailManual.Clear();

        //    foreach (WFRuleDetailManual oItem in truleDetailmn)
        //    {
        //        oRuleDetailManual.Add(oItem);
        //    }
        //    return oRuleDetailManual;
        //}

        //public List<WFRuleDetailNode> RemoveaSequence(List<WFRuleDetailNode> oRuleDetailNodes, int nSquenceID)
        //{
        //    List<WFRuleDetailNode> truleDetailNodes = new List<WFRuleDetailNode>();
        //    foreach (WFRuleDetailNode oItem in oRuleDetailNodes)
        //    {
        //        if (oItem.SequenceId != nSquenceID)
        //        {
        //            truleDetailNodes.Add(oItem);
        //        }
        //    }

        //    oRuleDetailNodes.Clear();

        //    foreach (WFRuleDetailNode oItem in truleDetailNodes)
        //    {
        //        oRuleDetailNodes.Add(oItem);
        //    }
        //    return oRuleDetailNodes;
        //}

        //public List<WFRuleDetailDesignation> RemoveaSequence(List<WFRuleDetailDesignation> oRuledesignations, int nSquenceID)
        //{
        //    List<WFRuleDetailDesignation> truledesignations = new List<WFRuleDetailDesignation>();
        //    foreach (WFRuleDetailDesignation oItem in oRuledesignations)
        //    {
        //        if (oItem.SequenceId != nSquenceID)
        //        {
        //            truledesignations.Add(oItem);
        //        }
        //    }

        //    oRuledesignations.Clear();

        //    foreach (WFRuleDetailDesignation oItem in truledesignations)
        //    {
        //        oRuledesignations.Add(oItem);
        //    }
        //    return oRuledesignations;
        //}

        //private List<OrganogramBasic> GetApprover(OrganogramBasic oNode, int nTier)
        //{
        //    List<OrganogramBasic> opprovers = new List<OrganogramBasic>();
        //    int nCount = 0;
        //    int nTop = 0;
        //    switch (this.WFRuleType)
        //    {
        //        case EnumWFRuleType.StraightAuthority:
        //            break;
        //        case EnumWFRuleType.FixedOGNode:
        //            foreach (WFRuleDetailNode oItem in this.WFRuleDetailNodes)
        //            {
        //                if (oItem.SequenceId == nTier)
        //                {
        //                    OrganogramBasic obasic = OrganogramBasic.Get(oItem.NodeId);
        //                    opprovers.Add(obasic);
        //                }
        //            }
        //            break;
        //        case EnumWFRuleType.Designation:
        //            #region Designation workflow
        //            nCount = 0;
        //            foreach (WFRuleDetailDesignation oItem in this.WFRuleDeatilDesignations)
        //                if (oItem.SequenceId == nTier)
        //                    nCount = nCount + 1;

        //            int[] designationIDs = new int[nCount];
        //            nTop = 0;
        //            for (int i = 0; i < this.WFRuleDeatilDesignations.Count; i++)
        //            {
        //                if (this.WFRuleDeatilDesignations[i].SequenceId == nTier)
        //                {
        //                    designationIDs[nTop] = this.WFRuleDeatilDesignations[i].DesignationId.Integer;
        //                    nTop = nTop + 1;
        //                }
        //            }

        //            if (designationIDs.Length == 0)
        //                throw new ServiceException("Approver not found in the Organogram setup, Contact your System Administrator");

        //            OrganogramBasic oApprover = oNode;
        //            bool bBreak = false;
        //            while (oApprover.Parent != null && bBreak != true)
        //            {
        //                oApprover = oApprover.Parent;
        //                if (oApprover.EmployeeAssigned == true)
        //                {
        //                    for (int i = 0; i < nTop; i++)
        //                    {
        //                        if (designationIDs[i] == oApprover.DesignationID.Integer)
        //                        {
        //                            opprovers.Add(oApprover);
        //                            bBreak = true;
        //                            break;
        //                        }
        //                    }
        //                }

        //            }
        //            #endregion Designation workflow

        //            break;
        //        case EnumWFRuleType.OrganizationStructure:
        //            #region Department Workflow
        //            foreach (WFRuleDetailOS oItem in this.WFRuleDeatilOSs)
        //                if (oItem.SequenceId == nTier)
        //                    nCount = nCount + 1;

        //            int[] depIDs = new int[nCount];
        //            nTop = 0;
        //            for (int i = 0; i < this.WFRuleDeatilOSs.Count; i++)
        //            {
        //                if (this.WFRuleDeatilOSs[i].SequenceId == nTier)
        //                {
        //                    depIDs[nTop] = this.WFRuleDeatilOSs[i].OsID.Integer;
        //                    nTop = nTop + 1;
        //                }
        //            }

        //            if (depIDs.Length == 0)
        //                throw new ServiceException("Approver not found in the Organogram setup, Contact your System Administrator");

        //            OrganogramBasic oogApprover;
        //            oogApprover = oNode;
        //            bool bogBreak = false;
        //            while (oogApprover.Parent != null && bogBreak != true)
        //            {
        //                oogApprover = oogApprover.Parent;
        //                if (oogApprover.EmployeeAssigned == true)
        //                {
        //                    for (int i = 0; i < nTop; i++)
        //                    {
        //                        if (depIDs[i] == oogApprover.DepartmentID.Integer)
        //                        {
        //                            opprovers.Add(oogApprover);
        //                            bogBreak = true;
        //                            break;
        //                        }
        //                    }
        //                }
        //            }
        //            #endregion Department Workflow
        //            break;

        //        case EnumWFRuleType.Manual:
        //            #region Department Workflow
        //            opprovers = new List<OrganogramBasic>();
        //            foreach (WFRuleDetailManual oItem in this.WFRuleDetailManual)
        //            {
        //                if (oItem.SequenceId == nTier)
        //                {
        //                    switch (oItem.RuleType)
        //                    {
        //                        case enumWFRuleManual.Employee:
        //                            break;
        //                        case enumWFRuleManual.Line_Manager:
        //                            if (oNode.ParentID != null)
        //                            {
        //                                OrganogramBasic lm = OrganogramBasic.Get(oNode.ParentID);
        //                                if (lm != null)
        //                                    opprovers.Add(lm);
        //                            }
        //                            break;
        //                        case enumWFRuleManual.Positions_type:
        //                            opprovers = OrganogramBasic.GetbyPositionTypeid(Ease.CoreV35.Model.(oItem.ObjectID));
        //                            if (opprovers != null && opprovers.Count > 1)
        //                            {
        //                                List<OrganogramBasic> lineApps = new List<OrganogramBasic>();
        //                                OrganogramBasic oparent = oNode.Parent;
        //                                while (true)
        //                                {
        //                                    if (oparent == null) break;
        //                                    OrganogramBasic obasic = opprovers.Find(delegate(OrganogramBasic ft) { return ft.ID.Integer == oparent.ID.Integer; });
        //                                    if (obasic != null)
        //                                    {
        //                                        lineApps.Add(obasic);
        //                                        break;
        //                                    }
        //                                    oparent = oparent.Parent;
        //                                }
        //                                opprovers = lineApps;
        //                            }
        //                            break;
        //                        case enumWFRuleManual.Og_Nodes:
        //                            if (oNode.ParentID != null)
        //                            {
        //                                OrganogramBasic og = OrganogramBasic.Get((oItem.ObjectID));
        //                                if (og != null)
        //                                    opprovers.Add(og);
        //                            }
        //                            break;

        //                        default:
        //                            break;
        //                    }
        //                }
        //            }


        //            if (opprovers == null || opprovers.Count == 0)
        //                throw new ServiceException("Approver not found in the Organogram setup, Contact your System Administrator");

        //            #endregion Department Workflow
        //            break;
        //        default:
        //            break;
        //    }

        //    if (opprovers.Count == 0)
        //        throw new ServiceException("Approver not found for the node:" + oNode.PositionName);

        //    return opprovers;
        //}

        //public List<OrganogramBasic> Approver(OrganogramBasic oNode, int nTier)
        //{
        //    List<OrganogramBasic> oApprovers = null;
        //    switch (this.WFRuleType)
        //    {
        //        case EnumWFRuleType.StraightAuthority:
        //            break;
        //        case EnumWFRuleType.OrganizationStructure:
        //            if (this.IsNextTier(nTier)) oApprovers = this.GetApprover(oNode, nTier);
        //            else throw new ServiceException("Approver not found for the node :" + oNode.PositionName + "; Rule: Dynamic");
        //            break;
        //        case EnumWFRuleType.FixedOGNode:
        //            if (this.IsNextTier(nTier)) oApprovers = this.GetApprover(oNode, nTier);
        //            else throw new ServiceException("Approver not found for the node:" + oNode.PositionName + "; Rule: Fixed node");
        //            break;
        //        case EnumWFRuleType.Designation:
        //            if (this.IsNextTier(nTier)) oApprovers = this.GetApprover(oNode, nTier);
        //            else throw new ServiceException("Approver not found for the node:" + oNode.PositionName + "; Rule: Designation");
        //            break;

        //        case EnumWFRuleType.Manual:
        //            if (this.IsNextTier(nTier)) oApprovers = this.GetApprover(oNode, nTier);
        //            else throw new ServiceException("Approver not found for the node:" + oNode.PositionName + "; Rule: Designation");
        //            break;

        //        default:
        //            throw new ServiceException("Approver not found for the node:" + oNode.PositionName);
        //            break;
        //    }
        //    return oApprovers;
        //}

        //public string GetRuleDetailDesignationDescription(int nSequenceID)
        //{
        //    string desc = string.Empty;
        //    foreach (WFRuleDetailDesignation oItem in this.WFRuleDeatilDesignations)
        //        if (oItem.SequenceId == nSequenceID) desc = desc + oItem.Designation.Name + ", ";
        //    if (WFRuleDeatilDesignations.Count >= 1) desc = desc.Substring(0, desc.Length - 2);
        //    return desc;
        //}

        //public string GetRuleDetailOSDescription(int nSequenceID)
        //{
        //    string desc = string.Empty;
        //    foreach (WFRuleDetailOS oItem in this.WFRuleDeatilOSs)
        //        if (oItem.SequenceId == nSequenceID) desc = desc + oItem.Department.Name + ", ";
        //    if (WFRuleDeatilOSs.Count >= 1) desc = desc.Substring(0, desc.Length - 2);
        //    return desc;
        //}

        //public string GetRuleDetailNodeDescription(int nSequenceID)
        //{
        //    string desc = string.Empty;
        //    foreach (WFRuleDetailNode oItem in WFRuleDetailNodes)
        //        if (oItem.SequenceId == nSequenceID) desc = desc + oItem.OrganogramBasic.PositionName + ", ";
        //    if (WFRuleDetailNodes.Count >= 1) desc = desc.Substring(0, desc.Length - 2);
        //    return desc;
        //}

        //public string GetRuleDetailManual(int nSequenceID)
        //{
        //    string desc = string.Empty;
        //    foreach (WFRuleDetailManual oItem in WFRuleDetailManual)
        //    {
        //        if (oItem.SequenceId == nSequenceID)
        //        {
        //            if (oItem.RuleType == enumWFRuleManual.Employee)
        //                desc = desc + " Any Employee" + ", ";
        //            else if (oItem.RuleType == enumWFRuleManual.Line_Manager)
        //                desc = desc + " Lime Manager" + ", ";
        //            else if (oItem.RuleType == enumWFRuleManual.Og_Nodes)
        //            {
        //                OrganogramBasic obasic = OrganogramBasic.Get((oItem.ObjectID));
        //                if (obasic == null)
        //                    desc = desc + " (Error), ";
        //                else desc = desc + obasic.PositionName + ", ";
        //            }
        //            else
        //            {
        //                OGPositionType obasic = OGPositionType.Get((oItem.ObjectID));
        //                desc = desc + obasic.Name + ", ";
        //            }
        //        }
        //    }
        //    if (WFRuleDetailManual.Count >= 1) desc = desc.Substring(0, desc.Length - 2);
        //    return desc;
        //}
        //#endregion
    }
}