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


namespace Baza_Reklam
{
    public partial class OferForm : Form
    {
        int customerId;
        int ofertaId;
        bool nowa = false;

        public int CustomerId
        {
            get { return customerId; }
            set { customerId = value; }
        }
       
        public OferForm(int custId,int ofertaID,bool nowa)
        {
            InitializeComponent();
            this.customerId = custId;
            this.ofertaId = ofertaID;
            this.nowa = nowa;

            oFERTY_MAINTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            oferty_WersjeTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            kLIENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            rEKLAMATableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            pRODUKCJATableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            jezyki_defTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            lISTA_TYTULOWTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            aGENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
        }

        private void OferForm_Load(object sender, EventArgs e)
        {
            if (this.nowa)
            {
                DataView table = (DataView) this.oFERTY_MAINBindingSource.List;
                DataRowView row = table.AddNew();

                row["CUSTOMER_ID"] = this.customerId;
                row["AGENT"] = User.Instance().Login;
                row["Data_Oferty"] = DateTime.Today;
                row["Data_Wyslania"] = DateTime.Today;
                row["Data_Modyfikacji"] = DateTime.Today;
                row["Zmodyfikowal_User"] = User.Instance().Login;

                this.oFERTY_MAINBindingSource.EndEdit();
                this.oFERTY_MAINTableAdapter.Update(this.rEKLAMADataSet.OFERTY_MAIN);
                this.oFERTY_MAINBindingSource.MoveLast();
            }
            else
            {              
                this.oFERTY_MAINTableAdapter.FillByIdOferty(this.rEKLAMADataSet.OFERTY_MAIN, this.ofertaId);
            }
 
            this.aGENCITableAdapter.Fill(this.sLOWNIKDataSet.AGENCI);

            this.kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU,this.customerId);
            this.kLIENCITableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI,this.customerId);
            this.rEKLAMATableAdapter.FillByCustomerId(this.rEKLAMADataSet.REKLAMA,this.customerId);

            foreach (DataRowView r in rEKLAMABindingSource.List)
            {
                this.pRODUKCJATableAdapter.ClearBeforeFill = false;
                this.pRODUKCJATableAdapter.FillByReklamaId(this.rEKLAMADataSet.PRODUKCJA,Convert.ToInt32(r["reklamaId"]));
            }

            this.jezyki_defTableAdapter.Fill(this.sLOWNIKDataSet.jezyki_def);
            this.oferty_WersjeTableAdapter.Fill(this.rEKLAMADataSet.Oferty_Wersje);
            this.lISTA_TYTULOWTableAdapter.Fill(this.sLOWNIKDataSet.LISTA_TYTULOW);          

            this.oFERTY_MAINBindingSource.ResetBindings(false);
           
        }

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

            this.oFERTY_MAINBindingSource.EndEdit();
            this.oFERTY_MAINTableAdapter.Update(this.rEKLAMADataSet.OFERTY_MAIN);

            DataRowView r = (DataRowView)oFERTY_MAINBindingSource.Current;
            //string katalog = @"C:\Users\Dorota\Desktop\Wzory_Pism\";
            string katalog1 = @"g:\reklamafiles\oferty\Wzory_Pism\";
            string katalog2 = @"g:\reklamafiles\oferty\";

           

            if (Convert.ToInt32(r["zapisana"]) == 0)
            {
                if (ofertyWersjeBindingSource.Current != null)
                {
                    DataRowView row = (DataRowView)ofertyWersjeBindingSource.Current;

                    string filename = katalog1 + row["nazwa_pliku"].ToString();
                    string newFileName = katalog2 + r["id_oferty"].ToString() + ".doc";

                    if (File.Exists(filename))
                    {
                        CreateNewOfer(filename, newFileName);

                        r["zapisana"] = -1;
                        oFERTY_MAINBindingSource.EndEdit();
                        oFERTY_MAINTableAdapter.Update(this.rEKLAMADataSet.OFERTY_MAIN);
                    }
                    else
                    {
                        MessageBox.Show("Nie istnieje wybrana oferta.");
                    }
                }       
            }
            else
            {
                string filename = katalog2 + r["id_oferty"].ToString() + ".doc";

                if (File.Exists(filename))
                {
                    OpenOfer(filename);
                }
                else
                {
                    MessageBox.Show("Nie można odnależć pliku z ofertą.");
                }               
            }
            this.Cursor = Cursors.Default;
        }

        private static void OpenOfer(string filename)
        {
            WordHelper wh = new WordHelper();

            wh.OpenFile(filename);
            wh.SetWordVisible();
        }

        private void CreateNewOfer(string filename, string newFileName)
        {
            WordHelper wh = new WordHelper();

            wh.OpenFile(filename);

            wh.FindAndReplace("<<Oferta_Numer>>", id_ofertyTextBox.Text + "/" + User.Instance().Kod_agenta.ToUpper() + "/" + DateTime.Today.Year);
            wh.FindAndReplace("<<Osoba_Do_Kontaktu>>", id_klienci_os_kontaktComboBox.Text);
            wh.FindAndReplace("<<Nazwa_i_Adres_Klienta>>", adres_KorTextBox.Text);
            wh.FindAndReplace("<<Miasto>>", User.Instance().Miasto);
            wh.FindAndReplace("<<Data_Oferty>>", data_OfertyDateTimePicker.Value.ToShortDateString());
            wh.FindAndReplace("<<Podpis_Agenta>>", User.Instance().Imie + " " + User.Instance().Nazwisko);
            wh.FindAndReplace("<<Email_Agenta>>", User.Instance().Email);
            wh.FindAndReplace("<<tel_wew_Agenta>>", User.Instance().TelWewnetrzny);
            wh.FindAndReplace("<<tel_mobile_Agenta>>", User.Instance().KomSluzbowa);
            wh.FindAndReplace("<<Adres_Biura>>", User.Instance().AdresBiura);
            wh.FindAndReplace("<<Tel_Biura>>", User.Instance().TelSekretariat);
            wh.FindAndReplace("<<Fax_Biura>>", User.Instance().FaxSekretariat);
            wh.FindAndReplace("<<Oferta_Opis_Dod>>", opis_DolTextBox.Text);
            wh.FindAndReplace("<<Stanowisko_Agenta>>", User.Instance().Stanowisko);

            wh.SaveAs(newFileName);

            wh.SetWordVisible();
        }

        private void zapiszButton_Click(object sender, EventArgs e)
        {
            DataRowView row = (DataRowView)oFERTY_MAINBindingSource.Current;
           
            row["Data_Modyfikacji"] = DateTime.Today;
            row["Zmodyfikowal_User"] = User.Instance().Login;

            this.oFERTY_MAINBindingSource.EndEdit();
            this.oFERTY_MAINTableAdapter.Update(this.rEKLAMADataSet.OFERTY_MAIN);
        }

        private void anulujButton_Click(object sender, EventArgs e)
        {
           this.rEKLAMADataSet.OFERTY_MAIN.RejectChanges();
        }
                
        private void id_tytulComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (id_tytulComboBox.SelectedValue != null)
            {
                ofertyWersjeBindingSource.Filter = "[id_tytul]=" + id_tytulComboBox.SelectedValue.ToString();
            }
        }

        private void OferForm_Shown(object sender, EventArgs e)
        {
            id_tytulComboBox.SelectedIndex = -1;
            oFERTY_MAINBindingSource.ResetBindings(false);
        }

    }
}