using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Baza_Reklam
{
    public partial class KalendarzEmisji : Form
    {
        //emisja, która ukazala sie w gazecie
        public readonly string WYDANA = "wydana";
        //emisja, ktora sie dopiero ukaze
        public readonly string NIEWYDANA = "niewydana";
        //emisja zafakturowana
        public readonly string ZAFAKTUROWANA = "zafakturowana";
        //emisja zafakturowana i wydana
        public readonly string ZAFAKTUROWANA_WYDANA = "zafakturowana_wydana";
        //emisja się nie ukazala, ale jest w miesiacu, ktory jest juz zafakturowany
        public readonly string MS_ZAFAKTUROWANY_NIEWYDANA = "zafakturowana_wydana";
        //emisja do zmiany
        public readonly string DO_ZMIANY = "do_zmiany";
        //emisja do zmiany
        public readonly string NOWA = "nowa";

        string tytul;

        REKLAMADataSet.REKLAMARow reklama;

        REKLAMADataSet.UKAZE_SIE_W_NRDataTable emisje;

        public REKLAMADataSet.UKAZE_SIE_W_NRDataTable Emisje
        {
            get { return emisje; }
            set { emisje = value; }
        }

        short emisjaDoZmiany;
        short nowaEmisja;

        int iloscEmisji;
        int index = -1;

        //TODO: tu trzeba bedzie wymyslec cos lepszego
        public DateTime dataOstatatniejZafakturowanejEmisji = DateTime.Parse("2008-12-30");

        #region properties

        public short NowaEmisja
        {
            get { return nowaEmisja; }
            set { nowaEmisja = value; }
        }

        public string Tytul
        {
            get { return tytul; }
            set { tytul = value; }
        }

        #endregion properties

        public KalendarzEmisji(string tytul, REKLAMADataSet.REKLAMARow reklama, REKLAMADataSet.UKAZE_SIE_W_NRDataTable emisje)
        {
            InitializeComponent();
            Tytul = tytul;
            label1.Text = tytul;
            this.reklama = reklama;
            this.emisje = emisje;

            foreach (DataRow r in emisje.Rows)
            {
                if (r.RowState != DataRowState.Deleted)
                {
                    iloscEmisji++;
                }
            }

            label2.Text = iloscEmisji.ToString();

            dodajButton.Click += dodajButton_Click;
        }


        public KalendarzEmisji(string tytul, REKLAMADataSet.REKLAMARow reklama, REKLAMADataSet.UKAZE_SIE_W_NRDataTable emisje, short emisjaDoZmiany)
        {
            InitializeComponent();
            Tytul = tytul;
            label1.Text = tytul;
            dodajButton.Text = "Zmień";

            this.reklama = reklama;
            this.emisje = emisje;

            this.emisjaDoZmiany = emisjaDoZmiany;

            dodajButton.Click += zmienButton_Click;
        }


        private void KalendarzEmisji_Load(object sender, EventArgs e)
        {
            nRTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            DateTime startDay = DateTime.Today;

            this.nRTableAdapter.FillByTytulData(this.sLOWNIKDataSet.NR, Tytul, startDay);
                       
            short nr = Emisje.MaxZafakturowanyNrWydania();
  
            if (nr != 0)
            {
                dataOstatatniejZafakturowanejEmisji = this.sLOWNIKDataSet.NR.dataWydania(Tytul, nr);
            }

            nRDataGridView.CellValueChanged += nRDataGridView_CellValueChanged;
            nRDataGridView.CellContentClick += nRDataGridView_CellContentClicked;

            nRBindingSource.ResetBindings(false);
        }

        private void dodajButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in nRDataGridView.Rows)
            {
                if (row.Cells[3].Value != null && ((bool)row.Cells[3].Value) == true)
                {
                    if (row.DefaultCellStyle.BackColor == Color.LightYellow)
                    {
                        REKLAMADataSet.UKAZE_SIE_W_NRRow em =
                        emisje.NewUKAZE_SIE_W_NRRow();

                        em.ReklamaId = reklama.ReklamaID;
                        em.dataDodania = DateTime.Today;
                        em.Nr_Wydania = Convert.ToInt16(row.Cells[0].Value);

                        emisje.AddUKAZE_SIE_W_NRRow(em);
                    }
                }
            }
        }

        private void zmienButton_Click(object sender, EventArgs e)
        {
            foreach (REKLAMADataSet.UKAZE_SIE_W_NRRow r in emisje)
            {
                if (r.RowState != DataRowState.Deleted)
                {
                    if (r.Nr_Wydania == emisjaDoZmiany)
                    {
                        r.Nr_Wydania = nowaEmisja;
                        r.EndEdit();
                        break;
                    }
                }
            }
        }

        private void nRDataGridView_CellContentClicked(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (!nRDataGridView.Rows[e.RowIndex].ReadOnly)
                {
                    if (e.ColumnIndex == 3)
                    {
                        if (nRDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor == Color.White)
                        {
                            if (emisjaDoZmiany != 0 && index != -1)
                            {
                                nRDataGridView.Rows[index].Cells[3].Value = false;
                                nRDataGridView.Rows[index].DefaultCellStyle.BackColor = Color.White;
                            }

                            nowaEmisja = (short)nRDataGridView.Rows[e.RowIndex].Cells[0].Value;
                            index = e.RowIndex;

                            nRDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightYellow;
                        }
                        else
                        {
                            nowaEmisja = (short)nRDataGridView.Rows[e.RowIndex].Cells[0].Value;
                            index = e.RowIndex;

                            nRDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
                        }

                    }
                }
            }
            nRDataGridView.EndEdit();
        }

        private void nRDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                if (((bool)nRDataGridView[e.ColumnIndex, e.RowIndex].Value) == true)
                {
                    iloscEmisji++;
                }
                else
                {
                    iloscEmisji--;
                }

                label2.Text = iloscEmisji.ToString();
            }
        }

        private void nRDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.Reset)
            {
                foreach (DataGridViewRow row in nRDataGridView.Rows)
                {
                    row.DefaultCellStyle.BackColor = Color.White;

                    row.Cells[2].Value = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames
                        [(int)Convert.ToDateTime(row.Cells[1].Value).DayOfWeek];

                    DataRow[] table = emisje.Select("[Nr Wydania]=" + row.Cells[0].Value);

                    if (table != null && table.Length > 0)
                    {
                        row.ReadOnly = true;
                        row.Cells[3].Value = true;
                        row.DefaultCellStyle.BackColor = Color.WhiteSmoke;

                        REKLAMADataSet.UKAZE_SIE_W_NRRow em =
                            (REKLAMADataSet.UKAZE_SIE_W_NRRow)table[0];

                        if (Convert.ToDateTime(row.Cells[1].Value) < DateTime.Today)
                        {
                            row.DefaultCellStyle.BackColor = Color.MintCream;
                        }
                        else
                        {
                            if (em.zafakturowana)
                            {
                                row.DefaultCellStyle.BackColor = Color.LightGray;
                            }

                            //anulowana
                            if (em.status == 2)
                            {
                                row.DefaultCellStyle.ForeColor = Color.Red;
                            }

                            if (emisjaDoZmiany == (short)row.Cells[0].Value)
                            {
                                row.DefaultCellStyle.BackColor = Color.MistyRose;
                            }
                        }
                    }
                    else if (emisjaDoZmiany == 0)
                    {
                        //emisja nie została wcześniej wybrana, ale jej misiąc już
                        // jest zafakturowany
                        DateTime dataWydania = (DateTime)row.Cells[1].Value;
                        if (dataWydania.Year <= dataOstatatniejZafakturowanejEmisji.Year
                            &&
                            dataWydania.Month <= dataOstatatniejZafakturowanejEmisji.Month)
                        {
                            row.ReadOnly = true;
                            row.DefaultCellStyle.BackColor = Color.LightGray;
                        }
                    }
                }
            }
        }
    }
}