using ETC2019.Controllers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ETC2019.Views
{
    public partial class BarcodePage : Form
    {
        private BarcodeController Controller { get; set; }
        int switch_turn = 0;
        int x_sec = 0;
        int y_sec = 0;
        int x_f = 0;
        int y_f = 0;
        float size_h = 0;
        float size_w = 0;
        public BarcodePage()
        {
            InitializeComponent();

            Controller = new BarcodeController();
        }

        private void CodePage_Resize(object sender, EventArgs e)
        {
            Text = string.Format("{0} x {1}", ClientSize.Width, ClientSize.Height);

            try
            {
                Clipboard.SetText(string.Format("{0}, {1}", Size.Width, Size.Height));
            }
            catch { }
        }

        private void ToolStrip1_Resize(object sender, EventArgs e)
        {
            int width = Controller.AvailableWidth(toolStrip1.Size.Width - toolStrip1.Padding.Left - toolStrip1.Padding.Right, toolStrip1.Items);

            toolStripTextBox1.Size = new Size(width, toolStripTextBox1.Size.Height);
        }

        private void ToolStripButton1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK && Controller.LoadFolder(folderBrowserDialog1.SelectedPath, out string[] items))
            {
                listBox1.Items.Clear();

                listBox1.Items.AddRange(items);
        
            }
        }

        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bitmap img = Controller.GetImage(listBox1.SelectedIndex);

            int x = img.Width;                  //取得"原圖的寬"數值
            int y = img.Height;                 //取得"原圖的高"數值

            pictureBox1.Image =  img;
            int h = pictureBox1.Height;         //取得"顯示的寬"數值
            int w = pictureBox1.Width;          //取得"顯示的高"數值
            size_w = (float)w / x;              //取得寬比例  
            size_h = (float)h / y;              //取得高比例
            label1.Text = "原圖"+ y + "顯示"+h+"比例:"+ size_h*100+"%";   //秀你好看
            
        }

        private void SplitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void PictureBox1_MouseClick(object sender, MouseEventArgs e)
        {


            Bitmap img = Controller.GetImage(listBox1.SelectedIndex);   //取圖片
            if (switch_turn == 1)                                       //第二次模式
            {
                x_sec = int.Parse(Controller.Click_pic_x(e));     //第二次X位置
                y_sec = int.Parse(Controller.Click_pic_y(e));     //第二次Y位置
                label3.Text = "滑鼠x:" + x_sec + "  滑鼠y:" + y_sec;    //顯示結果
                switch_turn = 0;                                        //切換第一次跟第二次點擊的變數紀錄
            }
            else                                                   //第一次
            {
                x_f = int.Parse(Controller.Click_pic_x(e));
                y_f = int.Parse(Controller.Click_pic_y(e));
                label2.Text = "滑鼠x:" + x_f + "  滑鼠y:" + y_f;
                switch_turn = 1;

            }
            if (switch_turn == 0)                                       //第二次點擊完 switch_turn會轉為零 進來輸出條碼結果
            {
                pictureBox1.Image = Controller.Rectangle_pic(x_f, y_f, x_sec, y_sec, size_w, size_h, img, out string result);//(x1,y1,x2,y2,縮小比例寬,縮小比例高,圖檔,黑科技我不懂)

                label4.Text = result;       //黑科技輸出

            }
        }

        private void Label2_Click(object sender, EventArgs e)
        {

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            
        }
    }
}
