using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Baza_Reklam.Classes.Helpers;
using Baza_Reklam.Classes.Interfaces;
using Baza_Reklam.Classes.Model;
using Baza_Reklam.Classes.Repositories;
using Baza_Reklam.Properties;

namespace Baza_Reklam
{
    public partial class MDIBazaReklam : Form
    {
        private readonly Dictionary<string, IForm> _forms = new Dictionary<string, IForm>();


        public MDIBazaReklam()
        {
            InitializeComponent();

            Text = "Baza reklam " + Application.ProductVersion;
        }

        private void MDIBazaReklam_Load(object sender, EventArgs e)
        {
            //Podmiana ConnectionStringa, ktory jest zapisany na stale w design time 
            Settings.Default["Baza_ReklamConnectionString"] = ConnString.getConnString().Value;

            uprawnienia();
        }

        private void uprawnienia()
        {
            startToolStripButton.Enabled = false;
            zamowieniaToolStripButton.Enabled = false;
            klienciToolStripButton.Enabled = false;
            fakturyToolStripButton.Enabled = false;
            raportyToolStripButton.Enabled = false;
            prowizjeToolStripButton.Enabled = false;
            infoToolStripButton.Enabled = false;
            produkcjaToolStripButton.Enabled = false;
            listaReklamToolStripButton.Enabled = false;


            if (User.Instance().IsHandlowiec || User.Instance().IsKierownik || User.Instance().IsSekretarka || User.Instance().IsProdukcja)
            {
                zamowieniaToolStripButton.Enabled = true;
                klienciToolStripButton.Enabled = true;
                fakturyToolStripButton.Enabled = true;
                listaReklamToolStripButton.Enabled = true;
            }

            if (User.Instance().IsDtp || User.Instance().IsHandlowiec || User.Instance().IsKierownik || User.Instance().IsSekretarka || User.Instance().IsProdukcja)
            {
                raportyToolStripButton.Enabled = true;
                infoToolStripButton.Enabled = true;
            }

            if (User.Instance().IsAdmin || User.Instance().IsHandlowiec || User.Instance().IsKierownik || User.Instance().IsSekretarka)
            {
                prowizjeToolStripButton.Enabled = true;
            }

            if (User.Instance().IsAdmin)
            {
                adminToolStripButton.Visible = true;
            }

            produkcjaToolStripButton.Enabled = true;


            if (User.Instance().IsHandlowiec || User.Instance().IsKierownik || User.Instance().IsSekretarka || User.Instance().IsProdukcja)
            {
                startToolStripButton.Enabled = true;

                if (!_forms.ContainsKey("StartForm"))
                    _forms.Add("StartForm", StartForm.getStartForm(this));
                StartForm.getStartForm(this).WindowState = FormWindowState.Maximized;
                StartForm.getStartForm(this).Show();
            }

            BindUserAgency();

        }

        private void klienciToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("ClientsForm"))
                _forms.Add("ClientsForm", ClientsForm.getClientsForm(this));
            ClientsForm.getClientsForm(this).WindowState = FormWindowState.Maximized;
            ClientsForm.getClientsForm(this).Show();

            Cursor = Cursors.Default;

        }

        private void zamowieniaToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("OrdersForm"))
                _forms.Add("OrdersForm", OrdersForm.getOrderForm(this));
            OrdersForm.getOrderForm(this).WindowState = FormWindowState.Maximized;
            OrdersForm.getOrderForm(this).Show();

            Cursor = Cursors.Default;

        }

        private void fakturyToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("FacturesForm"))
                _forms.Add("FacturesForm", FacturesForm.getFacturesForm(this));
            FacturesForm.getFacturesForm(this).WindowState = FormWindowState.Maximized;
            FacturesForm.getFacturesForm(this).Show();

            Cursor = Cursors.Default;

        }

        private void produkcjaToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("ProductionForm"))
                _forms.Add("ProductionForm", ProductionForm.getProductionForm(this));
            ProductionForm.getProductionForm(this).WindowState = FormWindowState.Maximized;
            ProductionForm.getProductionForm(this).Show();

            Cursor = Cursors.Default;
        }

        private void infoToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("InfoForm"))
                _forms.Add("InfoForm", InfoForm.getInfoForm(this));

            InfoForm.getInfoForm(this).WindowState = FormWindowState.Maximized;
            InfoForm.getInfoForm(this).Show();

            Cursor = Cursors.Default;
        }

        private void raportyToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("ReportsForm"))
                _forms.Add("ReportsForm", ReportsForm.getReportsForm(this));

            ReportsForm.getReportsForm(this).WindowState = FormWindowState.Maximized;
            ReportsForm.getReportsForm(this).Show();

            Cursor = Cursors.Default;
        }

        private void prowizjeToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("ProvisionForm"))
                _forms.Add("ProvisionForm", ProvisionForm.GetProvisionForm(this));

            ProvisionForm.GetProvisionForm(this).WindowState = FormWindowState.Maximized;
            ProvisionForm.GetProvisionForm(this).Show();

            Cursor = Cursors.Default;
        }

        private void startToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("StartForm"))
                _forms.Add("StartForm", StartForm.getStartForm(this));

            StartForm.getStartForm(this).WindowState = FormWindowState.Maximized;
            StartForm.getStartForm(this).Show();

            Cursor = Cursors.Default;
        }

        private void listaReklamToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("ListaReklamNaWydanieForm"))
                _forms.Add("ListaReklamNaWydanieForm", ListaReklamNaWydanieForm.GetListaReklamNaWydanieForm(this));

            ListaReklamNaWydanieForm.GetListaReklamNaWydanieForm(this).WindowState = FormWindowState.Maximized;
            ListaReklamNaWydanieForm.GetListaReklamNaWydanieForm(this).Show();

            Cursor = Cursors.Default;
        }

        private void adminToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("AdminForm"))
                _forms.Add("AdminForm", AdminForm.getAdminForm(this));

            AdminForm.getAdminForm(this).WindowState = FormWindowState.Maximized;
            AdminForm.getAdminForm(this).Show();

            Cursor = Cursors.Default;
        }

        private void zamowieniaToolStripButton2_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("ZestawienieZamowienForm"))
                _forms.Add("ZestawienieZamowienForm", ZestawienieZamowienForm.getZestawienieZamowienForm(this));

            ZestawienieZamowienForm.getZestawienieZamowienForm(this).WindowState = FormWindowState.Maximized;
            ZestawienieZamowienForm.getZestawienieZamowienForm(this).Show();

            Cursor = Cursors.Default;

        }

        private void fakturyNoweToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("FacturesFormNew"))
                _forms.Add("FacturesFormNew", FacturesFormNew.GetFacturesForm(this));

            FacturesFormNew.GetFacturesForm(this).WindowState = FormWindowState.Maximized;
            FacturesFormNew.GetFacturesForm(this).Show();

            Cursor = Cursors.Default;
        }

        private void korektaToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            foreach (Form childForm in MdiChildren)
            {
                childForm.Hide();
            }

            if (!_forms.ContainsKey("KorektyForm"))
                _forms.Add("KorektyForm", KorektyForm.getFacturesForm(this));

            KorektyForm.getFacturesForm(this).WindowState = FormWindowState.Maximized;
            KorektyForm.getFacturesForm(this).Show();

            Cursor = Cursors.Default;
        }

        private void tsbtnAbout_Click(object sender, EventArgs e)
        {
            //About.getAbout().WindowState = FormWindowState.Maximized;
            About about = new About();
            about.ShowDialog();
        }


        private void koniecToolStripButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void MDIBazaReklam_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Czy na pewno chcesz wyjść z programu?", "Baza Reklam", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
                return;
            }

            PdfHelper.CleanPdfFolder();

            e.Cancel = false;

            foreach (Form childForm in MdiChildren)
            {
                if (childForm != null) childForm.Close();
            }
        }

        private void MDIBazaReklam_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.Exit();
        }


        private void BindUserAgency()
        {
            cbUserAgency.Visible = User.Instance().IsKierownik && User.Instance().UserAgencyList.Count > 1;

            if (!cbUserAgency.Visible || cbUserAgency.ComboBox == null) return;

            List<UserAgency> userAgencies = UserAgencyRepository.FindByUserId(User.Instance().Id);
            
            cbUserAgency.ComboBox.DisplayMember = "Name";
            cbUserAgency.ComboBox.ValueMember = "AgencyId";
            cbUserAgency.ComboBox.DataSource = userAgencies;
            cbUserAgency.ComboBox.SelectedItem = userAgencies[0];
            cbUserAgency.SelectedIndexChanged += cbUserAgency_SelectedIndexChanged;
        }

        private void cbUserAgency_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbUserAgency.ComboBox == null || cbUserAgency.ComboBox.SelectedItem == null) return;

            User.Instance().IdAgencji = ((UserAgency) cbUserAgency.ComboBox.SelectedItem).AgencyId;
            User.Instance().SymbolAgencji = ((UserAgency)cbUserAgency.ComboBox.SelectedItem).AgencyName;

            foreach (string key in _forms.Keys)
                _forms[key].CloseForm();

            StartForm.getStartForm(this).WindowState = FormWindowState.Maximized;
            StartForm.getStartForm(this).Show();

        }
    }
}