using ProductionControl.Device;
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;

namespace ProductionControl.UI
{
    public partial class FrmScannerShow : Form
    {
        public Action reDraw;

        private Size size;

        private double ratio = 1;        // 图片的起始显示比例
        private double ratioStep = 0.1;
        private Size pic_size;
        private int xPos;
        private int yPos;

        ScannerDev scannerDev;
        public IntPtr picHwnd
        {
            get { return this.pictureBox1.Handle; }
        }
        public FrmScannerShow(Size _size, ScannerDev dev=null)
        {
            InitializeComponent();
            //this.WindowState = FormWindowState.Maximized;
            this.Width = Screen.PrimaryScreen.WorkingArea.Width / 3;
            this.Location = new Point(0, 0);
            size = _size;
            scannerDev = dev;
            if (scannerDev == null)
            {
                toolstrip1.Visible = numExposureTime.Visible= this.label1.Visible= false;
                this.ClientSize = new Size(this.ClientSize.Width,
                    (int)(this.ClientSize.Width * (size.Height * 1.0f / size.Width)));
                this.pictureBox1.Size = this.ClientSize;
                this.pictureBox1.Location = new Point(0, 0);
            }
            else
            {
                this.ClientSize = new Size(this.ClientSize.Width,
                    (int)(this.ClientSize.Width * (size.Height * 1.0f / size.Width))+ toolstrip1.Height);
                this.pictureBox1.Size = new Size(this.ClientSize.Width,this.ClientSize.Height- toolstrip1.Height);
                this.pictureBox1.Location = new Point(0, toolstrip1.Height);

                dev.getParam();
                this.numExposureTime.Value = (decimal)dev.ExposureTime;
            }
            
            this.Text = $"{(toolstrip1.Visible ? toolstrip1.Height:0)},{this.Height},{this.ClientSize.Height},{this.pictureBox1.Height},{this.pictureBox1.Top}";
            
        }

        private void FrmScannerShow_Load(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
            pictureBox1.MouseMove += pictureBox1_MouseMove;
            pictureBox1.MouseDown += pictureBox1_MouseDown;
            this.ActiveControl = this.pictureBox1; // 设置焦点   

            pic_size = this.pictureBox1.Size;
        }
        private void FrmScannerShow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (scannerDev != null)
            {
                scannerDev.stop();
                scannerDev.close();
                scannerDev = null;
            }
        }
        public void updateBmp(Bitmap bmp)
        {
            this.pictureBox1.Image = bmp;
        }
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            //Point pictureBoxPoint = this.PointToClient(Cursor.Position);
            //this.Text = $"{e.X}:{e.Y}; {pictureBoxPoint.X}:{pictureBoxPoint.Y}; {pictureBox1.Left}:{pictureBox1.Top}; " +
            //    $"{this.pictureBox1.Width}:{this.pictureBox1.Height}; {this.Width}:{this.Height}; " +
            //    $"Cursor:{Cursor.Position.X}:{Cursor.Position.Y}";
            if (e.Delta > 0)
            {
                ratio += ratioStep;
                if (ratio > 100) // 放大上限
                    ratio = 100;
                else
                {
                    int x = (int)(pictureBox1.Left - e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
                    int y = (int)(pictureBox1.Top - e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
 
                    this.changePictureBoxSize(new Point(x, y), ratio);
                }

            }
            else if (ratio - ratioStep >= 1)
            {
                ratio -= ratioStep;
                //if (ratio < 0.1)  // 放大下限
                //    ratio = 0.1;
                //else 
                int x = (int)(pictureBox1.Left + e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
                int y = (int)(pictureBox1.Top + e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
                this.changePictureBoxSize(new Point(x, y), ratio);
            }
        }
        private void changePictureBoxSize(Point location, double ratio)
        {
            var picSize = pictureBox1.Size;
            picSize.Width = Convert.ToInt32(pic_size.Width * ratio);
            picSize.Height = Convert.ToInt32(pic_size.Height * ratio);
            pictureBox1.Size = picSize;

            if (location.X > 0) location.X = 0;
            if (location.Y > 0) location.Y = 0;
            if (picSize.Width + location.X < this.ClientSize.Width) location.X = -(picSize.Width - this.ClientSize.Width);
            if (picSize.Height + location.Y < this.ClientSize.Height) location.Y = -(picSize.Height - this.ClientSize.Height);

            //Point location = new Point();
            //location.X = (this.ClientSize.Width - this.pictureBox1.Width) / 2;
            //location.Y = (this.ClientSize.Height - this.pictureBox1.Height) / 2;
            this.pictureBox1.Location = location;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                // 鼠标按下拖拽图片
                if (e.Button == MouseButtons.Left)
                { 
                    // 限制拖拽出框
                    if (pictureBox1.Width > this.ClientSize.Width || pictureBox1.Height > this.ClientSize.Height)
                    {
                        int moveX = e.X - xPos;
                        int moveY = e.Y - yPos;
                        if ((pictureBox1.Left + moveX) <= 0
                            && (pictureBox1.Top + moveY) <= 0
                            && (pictureBox1.Right + moveX) >= this.ClientSize.Width
                            && (pictureBox1.Bottom + moveY) >= this.ClientSize.Height)
                        {
                            pictureBox1.Left += moveX;//设置x坐标.
                            pictureBox1.Top += moveY;//设置y坐标. 
                        }
                    }
                }
            }
            catch (Exception dd)
            {
                MessageBox.Show(dd.Message);
            }
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            xPos = e.X;//当前x坐标.
            yPos = e.Y;//当前y坐标.
        }

        private void numExposureTime_ValueChanged(object sender, EventArgs e)
        {
            if(scannerDev != null && scannerDev.IsInit)
            {
                scannerDev.setParam((float)numExposureTime.Value);
            }
        } 

        private void tsbtnOpenDir_Click(object sender, EventArgs e)
        {
            if (scannerDev != null && scannerDev.IsInit && string.IsNullOrWhiteSpace(scannerDev.bmpSavePath))
                System.Diagnostics.Process.Start("explorer.exe", scannerDev.bmpSavePath+"\\"); 
        }

        private void tsbtnScnner_Click(object sender, EventArgs e)
        {
            if (scannerDev != null && scannerDev.IsInit)
                scannerDev.scan(1);

            System.Diagnostics.Process.Start("explorer.exe",Application.StartupPath+"\\temp\\");
        }
    }
}
