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

namespace hukpiWinApp
{
    public partial class MainForm : Form
    {
        private string loginUserID;
        private string loginUser;
        private string loginUserGroupID;
        private string loginUserFullName;
        private List<string> grantedModules;

        private bool isLogout = false;


        public MainForm(string loginUserID, string loginUser, string loginUserGroupID, string loginUserFullName)
        {
            InitializeComponent();

            this.loginUserID = loginUserID;
            this.loginUser = loginUser;
            this.loginUserGroupID = loginUserGroupID;
            this.loginUserFullName = loginUserFullName;

            setGrantedModulesArray(this.loginUserGroupID);
            showAccessableModules();
        }

        private void setGrantedModulesArray(string userGroup)
        {
            hukpiDatabaseDataSetTableAdapters.GroupsAndGrantedModulesTableAdapter grantedModules = new hukpiWinApp.hukpiDatabaseDataSetTableAdapters.GroupsAndGrantedModulesTableAdapter();
            hukpiDatabaseDataSet.GroupsAndGrantedModulesDataTable grantedModulesData = grantedModules.GetDataByGroupId(Convert.ToDecimal(userGroup));

            this.grantedModules = new List<string>();

            if (grantedModulesData.Count > 0)
            {
                foreach (hukpiDatabaseDataSet.GroupsAndGrantedModulesRow row in grantedModulesData)
                {
                    this.grantedModules.Add(row["moduleCode"].ToString());
                }
            }
        }

        private void showAccessableModules()
        {
            int[] grantedModuleCodes = getGrantedModuleCodes();

            foreach (System.Windows.Forms.ToolStripItem item in toolStrip.Items)
            {
                if (grantedModuleCodes.Contains(Convert.ToInt32(item.Tag)))
                {
                    item.Visible = true;
                }
                else if ("000".Equals(item.Tag))
                {
                    item.Visible = true;
                }
                else
                {
                    item.Visible = false;
                }
                    
            }
            
        }

        private int[] getGrantedModuleCodes()
        {
            hukpiDatabaseDataSetTableAdapters.GroupsAndGrantedModulesTableAdapter groupsAndGrantedModules = new hukpiWinApp.hukpiDatabaseDataSetTableAdapters.GroupsAndGrantedModulesTableAdapter();
            hukpiDatabaseDataSet.GroupsAndGrantedModulesDataTable grantedModules = groupsAndGrantedModules.GetDataByGroupId(Convert.ToDecimal(this.loginUserGroupID));

            int[] grantedModuleCodes = new int[grantedModules.Count];
            int index = 0;

            foreach (hukpiDatabaseDataSet.GroupsAndGrantedModulesRow row in grantedModules)
            {
                grantedModuleCodes[index++] = Convert.ToInt32(row["ModuleCode"]);
            }

            return grantedModuleCodes;
        }


        private void ShowNewForm(object sender, EventArgs e)
        {
            //Form childForm = new PartnerForm();
            //childForm.MdiParent = this;
            //childForm.Text = "Window " + childFormNumber++;
            //childForm.Show();
        }

        

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void toolStripButtonPartners_Click(object sender, EventArgs e)
        {
            this.openNewMDIChildForm(new SearchPartnerForm());
        }

        //private void toolStripButtonDepots_Click(object sender, EventArgs e)
        //{
        //    Form childForm = new DepotForm(this);
        //    childForm.MdiParent = this;
        //    childForm.Text = "Window " + childFormNumber++;
        //    childForm.Show();
        //}

        private void toolStripButtonAddPartner_Click(object sender, EventArgs e)
        {
            openNewMDIChildForm(new AddEditPartnerForm());
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!isLogout)
            {
                Application.Exit();
            }
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoginForm login = new LoginForm();
            login.Show();

            isLogout = true;
            this.Close();
        }

        public void refreshDataInAllChildForm()
        {
            foreach (Form form in this.MdiChildren)
            {
                try
                {
                    ((BaseForm)form).refreshData();
                }
                catch (Exception e)
                {
                    ;
                }
            }
        }

        public bool openNewMDIChildForm(Form formToOpen)
        {
            Form childForm = formToOpen;

            if (userHasRoleOnModule(childForm))
            {
                childForm.MdiParent = this;
                childForm.WindowState = FormWindowState.Maximized;
                childForm.Show();
                return true;
            }
            else
            {
                childForm.Dispose();
                return false;
            }
        }

        public List<string> getGrantedModules()
        {
            return this.grantedModules;
        }

        public bool userHasRoleOnModule(Form module)
        {
            string moduleTag;

            if (module.Tag == null || "".Equals(moduleTag = module.Tag.ToString().Trim()))
            {
                MessageBox.Show("Nincs a modulhoz jogosultság beállítva, egyeztessen a fejlesztővel!");
                return false;
            }
            else if (!this.grantedModules.Contains(moduleTag))
            {
                MessageBox.Show("Nincs jogosultsága a modul használatához!");
                return false;
            }

            return true;
            
        }

        private void toolStripButtonReports_Click(object sender, EventArgs e)
        {
            openNewMDIChildForm(new Forms.ReportListForm());
        }

        private void toolStripButtonUsers_Click(object sender, EventArgs e)
        {
            openNewMDIChildForm(new Forms.Admin.UserListForm());
        }

        private void toolStripButtonNewUser_Click(object sender, EventArgs e)
        {
            openNewMDIChildForm(new Forms.Admin.AddUserForm());
        }

        private void toolStripButtonGroups_Click(object sender, EventArgs e)
        {
            openNewMDIChildForm(new Forms.Admin.UserGroupListForm());
        }

        private void toolStripButtonNewUserGroup_Click(object sender, EventArgs e)
        {
            openNewMDIChildForm(new Forms.Admin.AddEditUserGroupForm());
        }

        private void toolStripButtonRoles_Click(object sender, EventArgs e)
        {
            openNewMDIChildForm(new Forms.Admin.RolesForm());
        }

        
    }
}
