using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aram_CRM_Project.BLL.Entity;

namespace Aram_CRM_Project.UIL
{
    public partial class frmStuff : Form
    {
        public frmStuff()
        {
            InitializeComponent();
        }

        private string _Mode = string.Empty;
        public string Mode
        {
            set { _Mode = value; }
            get { return _Mode; }
        }

        public void IdentifyMode(string Id)
        {
            Aram_CRM_Project.BLL.StuffCRUD StuffCrud = new Aram_CRM_Project.BLL.StuffCRUD();
            Stuff StuffVar = new Stuff();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();

            
            cboStuffGroup.DataSource = StuffCrud.GetStuffGroupForComboBox();
            cboStuffGroup.ValueMember = "Id";
            cboStuffGroup.DisplayMember = "Name";
            cboStuffGroup.SelectedIndex = -1;

            btnAddFeature.Enabled = false;

            switch (Mode)
            {
                case "New":
                    {
                        btnSelectEditFeature.Enabled = false;
                        btnDeleteFeature.Enabled = false;

                        this.Text = "  تعریف کالای جدید";
                        this.ShowDialog();
                        break;
                    }
                case "Edit":
                    {
                        StuffVar = StuffCrud.ReadStuff(Id);
                        if (StuffVar != null)
                        {
                            lblId.Text = StuffVar.Id;
                            txtName.Text = StuffVar.Name;
                            cboStuffGroup.SelectedValue = StuffVar.StuffGroupId;
                            txtComment.Text = StuffVar.Comment;
                            if (StuffVar.Feature != "")
                            {
                                string[] FeatureArray = StuffVar.Feature.Split('$');
                                for (int i = 0; i < FeatureArray.Length; i++)
                                {
                                    libFeature.Items.Add(FeatureArray[i]);
                                }
                                libFeature.SelectedIndex = 0;
                            }
                            else
                            {
                                btnSelectEditFeature.Enabled = false;
                                btnDeleteFeature.Enabled = false;
                            }
                            
                            this.Text = "  ویرایش کالا";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
                case "Show":
                    {
                        StuffVar = StuffCrud.ReadStuff(Id);
                        if (StuffVar != null)
                        {
                            lblId.Text = StuffVar.Id;
                            txtName.Text = StuffVar.Name;
                            cboStuffGroup.SelectedValue = StuffVar.StuffGroupId;
                            txtComment.Text = StuffVar.Comment;
                            string[] FeatureArray = StuffVar.Feature.Split('$');
                            for (int i = 0; i < FeatureArray.Length; i++)
                            {
                                libFeature.Items.Add(FeatureArray[i]);
                            }

                            txtFeature.Enabled = false;
                            btnSelectEditFeature.Enabled = false;
                            btnDeleteFeature.Enabled = false;
                            btnEnter.Enabled = false;
                            cboStuffGroup.DropDownStyle = ComboBoxStyle.Simple;
                            this.ActiveControl = this.btnExit;
                            this.Text = "  ویرایش کالا";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.StuffCRUD StuffCrud = new Aram_CRM_Project.BLL.StuffCRUD();
            Stuff StuffVar = new Stuff();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            frmMessage fMessage = new frmMessage();

            int Result;
            bool ErrorFlag = false;

            erpStuff.Clear();
            if (txtName.Text.Trim() == "")
            {
                erpStuff.SetIconAlignment(this.txtName, ErrorIconAlignment.MiddleLeft);
                erpStuff.SetError(this.txtName, Properties.Resources.ResourceManager.GetString("ErrEmptyName"));
                ErrorFlag = true;
            }
            if (cboStuffGroup.SelectedIndex == -1)
            {
                erpStuff.SetIconAlignment(this.cboStuffGroup, ErrorIconAlignment.MiddleLeft);
                erpStuff.SetError(this.cboStuffGroup, Properties.Resources.ResourceManager.GetString("ErrSelectStuffGroup"));
                ErrorFlag = true;
            }
            if (ErrorFlag)
            {
                return;
            }

            StuffVar.Id = lblId.Text;
            StuffVar.Name = utility.ChangeChar(txtName.Text.Trim());
            StuffVar.StuffGroupId = cboStuffGroup.SelectedValue.ToString();
            StuffVar.Comment = utility.ChangeChar(txtComment.Text.Trim());
            string FeatureArray = string.Empty;
            if (libFeature.Items.Count != 0)
            {
                for (int i = 0; i < libFeature.Items.Count; i++)
                {
                    FeatureArray = FeatureArray + libFeature.Items[i].ToString() + "$";
                }
                StuffVar.Feature = FeatureArray.Remove(FeatureArray.Length - 1);
            }
            
            
            switch (Mode)
            {
                case "New":
                    {
                        Result = StuffCrud.InsertStuff(StuffVar);
                        if (Result == 0)
                        {
                            fMessage.RegUnSuccess();
                            return;
                        }
                        else if (Result == -1)
                        {
                            fMessage.RepeatingRecord();
                            return;
                        }
                        break;
                    }
                case "Edit":
                    {
                        Result = StuffCrud.UpdateStuff(StuffVar);
                        if (Result == 0)
                        {
                            fMessage.RegUnSuccess();
                            return;
                        }
                        else if (Result == -1)
                        {
                            fMessage.RepeatingRecord();
                            return;
                        }
                        break;
                    }
            }
            this.Dispose();
        }

        private void btnAddFeature_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();

            if (txtFeature.Text.Trim() != "")
            {
                libFeature.Items.Add(utility.ChangeChar(txtFeature.Text.Trim()));
                txtFeature.Text = "";
                txtFeature.Focus();
            }

            if (libFeature.Items.Count == 0)
            {
                btnSelectEditFeature.Enabled = false;
                btnDeleteFeature.Enabled = false;
            }
            else
            {
                btnSelectEditFeature.Enabled = true;
                btnDeleteFeature.Enabled = true;
                libFeature.SelectedIndex = 0;
            }
        }

        private void txtFeature_TextChanged(object sender, EventArgs e)
        {
            if (txtFeature.Text.Trim() == "")
            {
                btnAddFeature.Enabled = false;
            }
            else
            {
                btnAddFeature.Enabled = true;
            }

            
            
        }

        

        private void btnDeleteFeature_Click(object sender, EventArgs e)
        {
            libFeature.Items.Remove(libFeature.SelectedItem);
            btnEditFeature.Visible = false;
            btnAddFeature.Visible = true;
            if (libFeature.Items.Count == 0)
            {
                btnSelectEditFeature.Enabled = false;
                btnDeleteFeature.Enabled = false;
            }
            else
            {
                libFeature.SelectedIndex = 0;
            }
        }

        private void btnSelectEditFeature_Click(object sender, EventArgs e)
        {
            lblItemId.Text = libFeature.SelectedIndex.ToString();
            txtFeature.Text = libFeature.SelectedItem.ToString();
            btnAddFeature.Visible = false;
            btnEditFeature.Visible = true;
            btnEditFeature.Location = new Point(btnAddFeature.Left, btnAddFeature.Top);
        }

        private void btnEditFeature_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();

            libFeature.Items[int.Parse(lblItemId.Text)] = utility.ChangeChar(txtFeature.Text.Trim());
            txtFeature.Text = "";
            btnEditFeature.Visible = false;
            btnAddFeature.Visible = true;
            txtFeature.Focus();
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void cboStuffGroup_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void txtFeature_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void txtComment_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

    }
}
