using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using TinyHeels.App_Code;

namespace TinyHeels
{
    public partial class ManageCategory : Form
    {
        public ManageCategory()
        {
            InitializeComponent();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtArticle_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int categoryId =Int32.Parse((string.IsNullOrWhiteSpace(txtCategoryId.Text)) ? "0" : txtCategoryId.Text);
                string categoryName = (string.IsNullOrWhiteSpace(txtCategoryNmae.Text)) ? "-" : txtCategoryNmae.Text;
                Category cat = new Category();
                cat.categoryId = categoryId;
                cat.categoryName = categoryName;
                cat.image = imageBytes;
                cat.addCategory();

                resetData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        byte[] imageBytes;
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog obj = new OpenFileDialog();
                obj.Filter = "Image File (*.jpg) | *.jpg";
                obj.FilterIndex = 1;
                DialogResult file = obj.ShowDialog();
                if(file==DialogResult.OK)
                {
                    pictureBox1.Image = Image.FromFile(obj.FileName);
                    imageBytes = imageToBytes(Image.FromFile(obj.FileName));
                }
            }
            catch (Exception ex)
            {

            }
        }
        public byte[] imageToBytes(System.Drawing.Image imgIn)
        {
            MemoryStream ms = new MemoryStream();
            imgIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
        public Image bytesToImage(Byte[] bytes)
        {
            MemoryStream ms = new MemoryStream(bytes);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

        private void ManageCategory_Load(object sender, EventArgs e)
        {
            loadCategories();
        }
        public void loadCategories()
        {
            try
            {
                Category cat = new Category();
                DataTable dt = cat.getAllCategories();
                categoryGrid.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void categoryGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int id =Int32.Parse(categoryGrid.SelectedRows[0].Cells[0].Value.ToString());
                string name = categoryGrid.SelectedRows[0].Cells[1].Value.ToString();

                txtCategoryNmae.Text = name;
                txtCategoryId.Text = id.ToString();

                imageBytes = (Byte[])(string.IsNullOrWhiteSpace(categoryGrid.SelectedRows[0].Cells[2].Value.ToString()) ? TinyHeels.Properties.Resources.default1 : categoryGrid.SelectedRows[0].Cells[2].Value);
                Image img = bytesToImage(imageBytes);
                pictureBox1.Image = img;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            resetData();
        }
        public void resetData()
        {
            txtCategoryId.Text = "";
            txtCategoryNmae.Text = "";
            pictureBox1.Image = TinyHeels.Properties.Resources.default1;
            categoryGrid.DataSource = null;
            loadCategories();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                YesNo y = new YesNo("Are You Sure? To Delete.");
                y.ShowDialog();
                if(Common.command)
                {
                    int id = Int32.Parse(categoryGrid.SelectedRows[0].Cells[0].Value.ToString());
                    Category cat = new Category();
                    cat.categoryId = id;
                    cat.deleteCategory();
                    resetData();
                }
                else
                {

                }

            }
            catch (Exception ex)
            {

            }
        }
    }
}
