using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Baza_Reklam.Classes;

namespace Baza_Reklam
{
    public partial class ListaReklamNaWydanieForm : Form
    {
        private static ListaReklamNaWydanieForm lrnwForm;

        public static ListaReklamNaWydanieForm getListaReklamNaWydanieForm(MDIBazaReklam parent)
        {
            if (lrnwForm == null)
            {
                lrnwForm = new ListaReklamNaWydanieForm(parent);
            }
            return lrnwForm;        
        }

        private ListaReklamNaWydanieForm(MDIBazaReklam parent)
        {
            InitializeComponent();

            this.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);
            
            ((ComboBox)this.tytToolStripComboBox2.Control).SelectedValueChanged += new EventHandler(
                tytToolStripComboBox2_SelectedIndexChanged);
        }

        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;
            }

            this.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", ((BoundItem)grzbietToolStripComboBox.SelectedItem).IDEvalue1);
            }

          //  ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).Checked = true;

         

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

            adapter.Fill(rEKLAMADataSet.LISTA_REKLAM_NA_WYDANIE);
            
            this.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 = Utils.customerId(idRek);

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

                this.Hide();

                ClientsForm.getClientsForm((MDIBazaReklam)this.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"]);
               // this.reklamaTableAdapter.FillByReklamaId(this.rEKLAMADataSet.REKLAMA, idRek);

                OrderDetails.getOrderDetails().pokazSzczegolyZamowienia(idRek);
                OrderDetails.getOrderDetails().ShowDialog();
                /*
                OrderDetails od = new OrderDetails();
                od.pokazSzczegolyZamowienia(idRek);
                od.ShowDialog(); */
            }     
        }

        private void tytToolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((ComboBox)this.tytToolStripComboBox2.Control).SelectedItem != null)
            {
                ((BindingSource)((ComboBox)this.modToolStripComboBox.Control).DataSource).Filter = "tytul = '" + ((ComboBox)this.tytToolStripComboBox2.Control).Text.ToString() + "'";
                this.modToolStripComboBox.SelectedIndex = -1;
                this.modToolStripComboBox.SelectedIndex = -1;
            }
        }


        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)
        {
            this.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 ";

            this.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ść.");
                    this.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ść.");
                    this.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);
                        
            this.Cursor = Cursors.Default; 
        }

        private void wyczyscToolStripButton2_Click(object sender, EventArgs e)
        {
            odToolStripTextBox.Clear();
            doToolStripTextBox.Clear();
            ((BindingSource)((ComboBox)this.modToolStripComboBox.Control).DataSource).Filter = "";          
            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 podgladJPGToolStripButton_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current != null)
            {
                this.Cursor = Cursors.WaitCursor;

                DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;

                int reklamaId = Convert.ToInt32(row["reklamaId"]);
                string idReklamy = row["id reklamy"].ToString();

                string tytul;
                int grzbiet;
                string miasto;
                byte staryNowyArchiwum;

                Produkcja.argumentyDoSciezki(reklamaId,idReklamy, out tytul, out grzbiet, out miasto, out staryNowyArchiwum);

                string sciezka = Produkcja.GetReklamaFileName(idReklamy, tytul, grzbiet, miasto, staryNowyArchiwum, "jpg");

                if (File.Exists(sciezka))
                {
                    System.Diagnostics.Process.Start(sciezka);
                }
                else
                {
                    MessageBox.Show("Nie znaleziono pliku: " + sciezka);
                }

                this.Cursor = Cursors.Default;
            }
        }

        private void podgladToolStripButton_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current != null)
            {
                this.Cursor = Cursors.WaitCursor;

                DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;

                int reklamaId = Convert.ToInt32(row["reklamaId"]);
                string idReklamy = row["id reklamy"].ToString();

                string tytul;
                int grzbiet;
                string miasto;
                byte staryNowyArchiwum;

                Produkcja.argumentyDoSciezki(reklamaId,idReklamy, out tytul, out grzbiet, out miasto, out staryNowyArchiwum);

                string sciezka = Produkcja.GetReklamaFileName(idReklamy, tytul, grzbiet, miasto, staryNowyArchiwum, "pdf");

                if (File.Exists(sciezka))
                {
                    System.Diagnostics.Process.Start(sciezka);
                }
                else
                {
                    MessageBox.Show("Nie znaleziono pliku: " + sciezka);
                }

                this.Cursor = Cursors.Default;
            }
        }

        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 = Utils.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;
                    }
                }
            }
        }

    }
}