using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using Baza_Reklam.Classes.Helpers;
using Baza_Reklam.Classes.Interfaces;

namespace Baza_Reklam
{
    public partial class ListaReklamNaWydanieForm : Form, IForm
    {
        private static ListaReklamNaWydanieForm _listaReklamNaWydanie;

        public static ListaReklamNaWydanieForm GetListaReklamNaWydanieForm(MDIBazaReklam parent)
        {
            if (_listaReklamNaWydanie == null)
            {
                _listaReklamNaWydanie = new ListaReklamNaWydanieForm(parent);
            }
            return _listaReklamNaWydanie;        
        }

        private ListaReklamNaWydanieForm(Form parent)
        {
            InitializeComponent();

            MdiParent = parent;
            listaReklamNaWydanie2TableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
         
            bindingNavigator1.Items.Insert(17, new ToolStripLabel("DR"));
            bindingNavigator1.Items.Insert(18, new ToolStripControlHost(new CheckBox(), "DRCheckBox"));
            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).Checked = true;

            bindingNavigator1.Items.Insert(19, new ToolStripLabel("A"));
            bindingNavigator1.Items.Insert(20, new ToolStripControlHost(new CheckBox(), "AktywneCheckBox"));
            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["AktywneCheckBox"]).Control).Checked = true;
       
            bindingNavigator1.Items.Insert(21, new ToolStripLabel("W"));
            bindingNavigator1.Items.Insert(22, new ToolStripControlHost(new CheckBox(), "WyrCheckBox"));
            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["WyrCheckBox"]).Control).Checked = false;

        }

        private void ListaReklamNaWydanieForm_Load(object sender, EventArgs e)
        {
            DBBindings.bindujTytuly(tytulToolStripComboBox);
            DBBindings.bindujTytuly(tytToolStripComboBox2);
            //DBBindings.bindujGrzbiety2(grzbietToolStripComboBox);
            DBBindings.bindujAgencje(agencjaToolStripComboBox);
            DBBindings.bindujAgentow(agentToolStripComboBox);
            //DBBindings.BindujModuly(modToolStripComboBox);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            rEKLAMADataSet.LISTA_REKLAM_NA_WYDANIE.Clear();

            if (tytulToolStripComboBox.Text.Trim() == "")
            {
                MessageBox.Show("Wybierz tytuł.");
                return;
            }

            if (nrToolStripTextBox.Text.Trim() != "")
            {
                int i;
                if (!Int32.TryParse(nrToolStripTextBox.Text.Trim(), out i))
                {
                    MessageBox.Show("Podaj prawidłowy nr wydania.");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Wybierz nr wydania.");
                return;
            }

            Cursor = Cursors.WaitCursor;

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConnString.getConnString().Value;

            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT TOP 1000 * FROM LISTA_REKLAM_NA_WYDANIE " +
                        " WHERE (TYTUŁ = @tytul) AND (NR = @nrwydania) ";
                       

            command.Parameters.AddWithValue("@tytul", tytulToolStripComboBox.Text.Trim());
            command.Parameters.AddWithValue("@nrwydania", nrToolStripTextBox.Text.Trim());

            if (((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).CheckState == CheckState.Checked)
            {
                command.CommandText += " AND ([ZATWIERDZONO DO DRUKU] = 1) ";
            }

            if (((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["AktywneCheckBox"]).Control).CheckState == CheckState.Checked)
            {
                command.CommandText += " AND ([status] <> 2) ";
            }

            if (((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).CheckState == CheckState.Checked)
            {
                command.CommandText += " AND ([ZATWIERDZONO DO DRUKU] = 1) ";
            }

            if (agencjaToolStripComboBox.Text.Trim() != "")
            {
                command.CommandText += " AND (agencja = @agencja) ";
                command.Parameters.AddWithValue("@agencja", agencjaToolStripComboBox.Text.Trim());
            }

            if (agentToolStripComboBox.Text.Trim() != "")
            {
                command.CommandText += " AND ([symbol akwizytora] = @agent) ";
                command.Parameters.AddWithValue("@agent", agentToolStripComboBox.Text.Trim());
            }

            if (grzbietToolStripComboBox.SelectedItem != null)
            {
                command.CommandText += " AND (GRZBIET = @grzbiet) ";
                command.Parameters.AddWithValue("@grzbiet", ((ComboItem)grzbietToolStripComboBox.SelectedItem).Value);
            }

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = command;

            adapter.Fill(rEKLAMADataSet.LISTA_REKLAM_NA_WYDANIE);
            
            Cursor = Cursors.Default;
        }

        private void klientToolStripButton_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current != null)
            {
                DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;
                int idRek = Convert.ToInt32(row["ReklamaId"]);

                int custId = DbUtils.customerId(idRek);

                ClientsForm.getClientsForm((MDIBazaReklam)MdiParent).pokazKlienta(custId);

                Hide();

                ClientsForm.getClientsForm((MDIBazaReklam)MdiParent).Show();
            }  
        }

        private void zamToolStripButton_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current != null)
            {
                DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;
                int idRek = Convert.ToInt32(row["ReklamaId"]);

                OrderDetails.getOrderDetails().PokazSzczegolyZamowienia(idRek);
                OrderDetails.getOrderDetails().ShowDialog();
            }     
        }

        private void wyczyscFiltrToolStripButton_Click(object sender, EventArgs e)
        {            
            tytulToolStripComboBox.Text = "";
            nrToolStripTextBox.Text = "";
            agencjaToolStripComboBox.SelectedIndex = -1;
            agencjaToolStripComboBox.Text = "";

            agentToolStripComboBox.SelectedIndex = -1; 
            agentToolStripComboBox.Text = "";

            grzbietToolStripComboBox.SelectedIndex = -1;
            grzbietToolStripComboBox.Text = "";

            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).CheckState = CheckState.Indeterminate;
            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["WyrCheckBox"]).Control).CheckState = CheckState.Indeterminate;          

        }

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

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConnString.getConnString().Value;

            SqlCommand command = new SqlCommand();
            command.Connection = conn;

            command.CommandText = "SELECT TOP 1000 * FROM LISTA_REKLAM_NA_WYDANIE WHERE 1=1 ";

            rEKLAMADataSet.LISTA_REKLAM_NA_WYDANIE.Clear();       

            command.Parameters.Clear();


            if (odToolStripTextBox.Text.Trim() != "")
            {
                int i;
                if (!Int32.TryParse(odToolStripTextBox.Text.Trim(), out i))
                {
                    MessageBox.Show("Podaj prawidłową wartość.");
                    Cursor = Cursors.Default;
                    return;
                }

                command.CommandText += " AND NR >=@od ";
                command.Parameters.AddWithValue("@od", odToolStripTextBox.Text.Trim());
            }


            if (doToolStripTextBox.Text.Trim() != "")
            {
                int i;
                if (!Int32.TryParse(doToolStripTextBox.Text.Trim(), out i))
                {
                    MessageBox.Show("Podaj prawidłową wartość.");
                    Cursor = Cursors.Default;
                    return;
                }

                command.CommandText += " AND NR <=@do ";
                command.Parameters.AddWithValue("@do", doToolStripTextBox.Text.Trim());
            }

            if (modToolStripComboBox.Text.Trim() != "")
            {
                command.CommandText += " AND MOD_TYP=@modul ";
                command.Parameters.AddWithValue("@modul", modToolStripComboBox.Text.Trim());
            }

            if (tytToolStripComboBox2.Text.Trim() != "")
            {
                command.CommandText += " AND TYTUŁ=@tyt ";
                command.Parameters.AddWithValue("@tyt", tytToolStripComboBox2.Text.Trim());

            }
          
            
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = command;

            adapter.Fill(rEKLAMADataSet.LISTA_REKLAM_NA_WYDANIE);
                        
            Cursor = Cursors.Default; 
        }

        private void wyczyscToolStripButton2_Click(object sender, EventArgs e)
        {
            odToolStripTextBox.Clear();
            doToolStripTextBox.Clear();
            if (modToolStripComboBox.ComboBox != null) 
                modToolStripComboBox.ComboBox.DataSource = new List<ComboItem>();
            modToolStripComboBox.SelectedIndex = -1;
            modToolStripComboBox.SelectedIndex = -1;
            modToolStripComboBox.Text = "";
            tytToolStripComboBox2.SelectedIndex = -1;
            tytToolStripComboBox2.SelectedIndex = -1;
            tytToolStripComboBox2.Text = "";
        }

        private void drukujToolStripButton_Click(object sender, EventArgs e)
        {
            PrintDGV.Print_DataGridView(listaReklamDataGridView, 50);
        }

        private void dodajLogoToolStripButton_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current != null)
            {              
                DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;

                int reklamaId = Convert.ToInt32(row["reklamaId"]);
                LogoWReklamieForm lwrf = new LogoWReklamieForm(reklamaId);

                lwrf.ShowDialog();
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current != null)
            {
                DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;
                int reklamaId = Convert.ToInt32(row["reklamaId"]);
                int custId = DbUtils.customerId(reklamaId);

                LogaKlientowForm logaForm = new LogaKlientowForm(custId);
                logaForm.ShowDialog();
            }
        }

        private void listaReklamDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.Reset)
            {
                foreach (DataGridViewRow r in listaReklamDataGridView.Rows)
                {
                    if (
                        !((REKLAMADataSet.LISTA_REKLAM_NA_WYDANIERow)
                        ((DataRowView)r.DataBoundItem).Row).ZATWIERDZONO_DO_DRUKU
                        ||
                        ((REKLAMADataSet.LISTA_REKLAM_NA_WYDANIERow)
                        ((DataRowView)r.DataBoundItem).Row).sTATUS == 2)
                    {

                        r.DefaultCellStyle.ForeColor = Color.Red;
                    }
                }
            }
        }

        private void podgladToolStripButton_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current == null) return;

            DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;
            string symbolReklamy = row["id reklamy"].ToString();

            Cursor = Cursors.WaitCursor;
            try
            {
                PdfHelper pdfHelper = new PdfHelper(symbolReklamy);
                if (!pdfHelper.DisplayPdf())
                    MessageBox.Show(pdfHelper.ErrorMessage);
            }
            catch (System.Net.WebException exception)
            {
                PdfHelper.HandleWebException(exception);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        public void CloseForm()
        {
            _listaReklamNaWydanie = null;
        }

        private void tytulToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (grzbietToolStripComboBox.ComboBox == null || tytulToolStripComboBox.ComboBox == null) return;
            
            grzbietToolStripComboBox.ComboBox.DataSource = new List<ComboItem>();
            grzbietToolStripComboBox.ComboBox.Text = string.Empty;

            if (tytulToolStripComboBox.ComboBox.SelectedItem == null || ((ComboItem)tytulToolStripComboBox.ComboBox.SelectedItem).Value == 0)
                return;
            
            DBBindings.BindujGrzbiety(grzbietToolStripComboBox, ((ComboItem)tytulToolStripComboBox.ComboBox.SelectedItem).Value);
        }
        

        private void tytToolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (modToolStripComboBox.ComboBox == null || tytToolStripComboBox2.ComboBox == null) return;
            
            modToolStripComboBox.ComboBox.DataSource = new List<ComboItem>();
            modToolStripComboBox.ComboBox.Text = string.Empty;

            if (tytToolStripComboBox2.ComboBox.SelectedItem == null ||
                ((ComboItem) tytToolStripComboBox2.ComboBox.SelectedItem).Value == 0)
                return;

            DBBindings.BindujModuly(modToolStripComboBox,
                                    ((ComboItem) tytToolStripComboBox2.ComboBox.SelectedItem).Display);
        }
    }
}