using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using mgh;

namespace Grocery
{
    public partial class FrmKala : Form
    {
        public enum Mod { NewRecord = 0, UpdateRecode = 1 };
        private Mod mod;
        List<Kala> KalasList = new List<Kala>();
        List<kalaGroup> GroupList = new List<kalaGroup>();
        int _pos, rc;
        bool newkala;
        public int position
        {
            set
            {
                _pos = value;
                EnableNavigator();
            }
            get
            {
                return _pos;
            }
        }
        public FrmKala()
        {
            InitializeComponent();
        }

        public FrmKala(bool nk)
        {
            InitializeComponent();
            newkala = nk;
        }
        private void Bind()
        {try{
            KalasList = Kala.GetAllKalas();
            GroupList = kalaGroup.GetAllGroups();
            cmbgroups.DataSource = GroupList;
            cmbgroups.DisplayMember = "groupname";
            cmbgroups.ValueMember = "groupname";
            //cmbgroups.AutoCompleteCustomSource = cmbgroups.Items. ;
            rc = KalasList.Count;
        }
        catch (Exception ex)
        {
            FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
        }
        }

        private void BindingControls()
        {
            try
            {
                txtprice.Text = KalasList[position].price.Trim();
                txtkalaid.Text = KalasList[position].kalaid.Trim();
                txtkalaname.Text = KalasList[position].kalaname.Trim();
                txtvahed.Text = KalasList[position].Vahed.Trim();
                cmbgroups.Text = KalasList[position].GroupName.Trim();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void EnableNavigator()
        {
            if (_pos == -1 || rc == 1) { Btn_First.Enabled = Btn_Last.Enabled = Btn_Next.Enabled = Btn_Prev.Enabled = false; }
            else if (_pos > 0 && _pos < rc - 1) { Btn_First.Enabled = Btn_Last.Enabled = Btn_Next.Enabled = Btn_Prev.Enabled = true; }
            else if (_pos == 0 && rc > 1) { Btn_First.Enabled = Btn_Prev.Enabled = false; Btn_Last.Enabled = Btn_Next.Enabled = true; }
            else if (_pos == rc - 1 && rc > 1) { Btn_First.Enabled = Btn_Prev.Enabled = true; Btn_Last.Enabled = Btn_Next.Enabled = false; }
        }

        private void EnableControls()
        {
            switch (mod)
            {
                case Mod.NewRecord:
                    Btn_Delete.Enabled = false;
                    Btn_First.Enabled = false;
                    Btn_Last.Enabled = false;
                    Btn_New.Enabled = false;
                    Btn_Next.Enabled = false;
                    Btn_Prev.Enabled = false;
                    Btn_Save.Enabled = true;
                    Btn_Update.Enabled = false;
                    txtkalaid.ReadOnly = false;
                    break;
                case Mod.UpdateRecode:
                    Btn_Delete.Enabled = true;
                    Btn_New.Enabled = true;
                    Btn_Save.Enabled = false;
                    Btn_Update.Enabled = true;
                    txtkalaid.ReadOnly = true;
                    EnableNavigator();
                    break;
            }
        }

        private void FrmKala_Load(object sender, EventArgs e)
        {
            //bar1.Text = "جستجو";
            Bind();
            if (rc > 0)
            {
                position = 0;
                BindingControls();
            }
            else position = -1;
            mod = Mod.UpdateRecode;
            EnableControls();
            if (newkala) Btn_New.PerformClick();
        }

        private void Btn_First_Click(object sender, EventArgs e)
        {
            position = 0;
            BindingControls();
        }
        private void Btn_Next_Click(object sender, EventArgs e)
        {
            position++;
            BindingControls();
        }

        private void Btn_Prev_Click(object sender, EventArgs e)
        {
            position--;
            BindingControls();
        }

        private void Btn_Last_Click(object sender, EventArgs e)
        {
            position = rc - 1;
            BindingControls();
        }


        private void Btn_New_Click(object sender, EventArgs e)
        {
            mod = Mod.NewRecord;
            EnableControls();
            txtkalaname.Text = txtkalaid.Text = string.Empty;
            txtprice.Text = "0";
            cmbgroups.SelectedIndex = 0;
            txtkalaid.Focus();
        }

        private void Btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();
                if (txtkalaname.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtkalaname, "لطفا این فیلد را پر کنید");
                    return;
                }
                if (txtprice.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtprice, "لطفا این فیلد را پر کنید");
                    return;
                }
                
                Kala ka = new Kala();
                ka.price = txtprice.Text;
                ka.kalaid = txtkalaid.Text;
                ka.kalaname = txtkalaname.Text;
                ka.Vahed = txtvahed.Text;
                ka.GroupName = cmbgroups.Text;
                ka.Update();
                FarsiMessagbox.Show("ذخیره شد", "مشخصات کالا", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);     
                Bind();
                EnableControls();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();
                if (txtkalaid.Text.Trim().Length != 3)
                {
                    errorProvider1.SetError(txtkalaid, "لطفا یک عدد سه رقمی وارد کنید");
                    return;
                }
                if (txtkalaname.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtkalaname, "لطفا این فیلد را پر کنید");
                    return;
                }
                if (txtprice.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtprice, "لطفا این فیلد را پر کنید");
                    return;
                }

                Kala ka = new Kala();
                ka.price = txtprice.Text;
                ka.kalaid = txtkalaid.Text;
                ka.kalaname = txtkalaname.Text;
                ka.Vahed = txtvahed.Text;
                ka.GroupName = cmbgroups.Text;
                ka.Insert();
                Bind();
                mod = Mod.UpdateRecode;
                EnableControls();
                FarsiMessagbox.Show("ذخیره شد", "مشخصات کالا", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                if (newkala) this.Close();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void Btn_ExitAction_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            mod = Mod.UpdateRecode;
            EnableControls();
            BindingControls();
        }

        private void FrmKala_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Insert)
                Btn_New.PerformClick();
            if (e.KeyCode == Keys.F1)
                Btn_ExitAction.PerformClick();
            if (e.KeyCode == Keys.F2)
                Btn_Update.PerformClick();
            if (e.KeyCode == Keys.F8)
                Btn_Save.PerformClick();
            if (e.KeyCode == Keys.F6)
                Btn_Delete.PerformClick();
            if (e.KeyCode == Keys.F4)
                groupPanel1.Focus();
            if (e.KeyCode == Keys.Home)
                Btn_First.PerformClick();
            if (e.KeyCode == Keys.End)
                Btn_Last.PerformClick();
            if (e.KeyCode == Keys.PageDown)
                Btn_Next.PerformClick();
            if (e.KeyCode == Keys.PageUp)
                Btn_Prev.PerformClick();
            if (e.KeyCode == Keys.Escape)
                this.Close();
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (FarsiMessagbox.Show("آیا مطمئن هستید؟", "", FMessageBoxButtons.YesNo, FMessageBoxIcon.Information) == DialogResult.Yes)
                {
                    KalasList[position].Delete();
                    Bind();
                    position--;
                    BindingControls();
                    EnableControls();
                    FarsiMessagbox.Show("حذف شد", "مشخصات کالا", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void txtkalaid_TextChanged(object sender, EventArgs e)
        {
            foreach (char ch in txtkalaid.Text.ToCharArray())
            {
                if (char.IsDigit(ch) == false && ch != '.') txtkalaid.Text = txtkalaid.Text.Replace(ch.ToString(), "");
            }
        }

        private void txtprice_TextChanged(object sender, EventArgs e)
        {
            foreach (char ch in txtprice.Text.ToCharArray())
            {
                if (char.IsDigit(ch) == false && ch != '.') txtprice.Text = txtkalaid.Text.Replace(ch.ToString(), "");
            }
        }

        private void bar1_ItemClick(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i;
            for ( i = 0; i < KalasList.Count; i++)
            {
                if (KalasList[i].kalaid == textBoxX1.Text)
                {
                    position = i;
                    BindingControls();
                    break;
                }
            }
            if (i==KalasList.Count) FarsiMessagbox.Show("پبدا نشد");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i;
            for (i = 0; i < KalasList.Count; i++)
            {
                if (KalasList[i].kalaname == textBoxX2.Text)
                {
                    position = i;
                    BindingControls();
                    break;
                }
            }
            if (i == KalasList.Count) FarsiMessagbox.Show("پبدا نشد");
        }

    }
}
