using _SCREEN_CAPTURE;
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 WeifenLuo.WinFormsUI.Docking;
using ZXing;

namespace 开发辅助工具.Tools
{
    public partial class FrmQrCode : DockContent
    {
        public FrmQrCode()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 生成二维码图片
        /// </summary>
        /// <param name="strMessage">要生成二维码的字符串</param>
        /// <param name="width">二维码图片宽度</param>
        /// <param name="height">二维码图片高度</param>
        /// <returns></returns>
        private Bitmap CreateQrCode(String strMessage, Int32 width, Int32 height)
        {
            Bitmap result = null;
            try
            {
                BarcodeWriter barCodeWriter = new BarcodeWriter
                {
                    Format = BarcodeFormat.QR_CODE
                };
                barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
                barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
                barCodeWriter.Options.Height = height;
                barCodeWriter.Options.Width = width;
                barCodeWriter.Options.Margin = 0;
                ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
                result = barCodeWriter.Write(bm);
                Bitmap qr_bg = (Bitmap)ryCommon.RyFiles.LoadPicFromFile(Application.StartupPath + "\\mod\\qr_bg.jpg");
                Graphics g = Graphics.FromImage(qr_bg);
                g.DrawImage(result, new Rectangle(25, 25, qr_bg.Width - 50, qr_bg.Height - 50));
                result = qr_bg;
            }
            catch (Exception)
            {
                //异常输出
            }
            return result;
        }
        private void BtnCreate_Click(object sender, EventArgs e)
        {
           pictureBox1.Image= CreateQrCode(TxtFromStr.Text, 256, 256);
        }

        private void BtnBrowser_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                TxtQrCodePath.Text = openFileDialog1.FileName;
                BtnAnaly.PerformClick();
            }
        }

        private void BtnAnaly_Click(object sender, EventArgs e)
        {
            string path = TxtQrCodePath.Text;
            if (!System.IO.File.Exists(path)) { Init(); return; }
            var img = ryCommon.RyFiles.LoadPicFromFile(path);
            if (img == null) { Init(); return; }
            Bitmap bmap;
            try
            {
                bmap = new Bitmap(img);
            }
            catch (System.IO.IOException ioe)
            {
                MessageBox.Show(ioe.ToString(),"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (bmap == null)
            {
                MessageBox.Show("不能解码图片","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                BarcodeReader reader = new BarcodeReader();
                reader.Options.CharacterSet = "UTF-8";
                var result = reader.Decode(bmap);
                TxtToResult.Text = (result == null) ? "" : result.Text;
                pictureBox2.Image = img;
            }
            catch (Exception)
            {
                Init();
                MessageBox.Show("解析二维码失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            void Init()
            {
                pictureBox2.Image = null; TxtToResult.Text = "";
            }
        }

        private void PictureBox1_Click(object sender, EventArgs e)
        {
            if(saveFileDialog1.ShowDialog()==DialogResult.OK)
            {
                try
                {
                    pictureBox1.Image.Save(saveFileDialog1.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void BtnCapture_Click(object sender, EventArgs e)
        {
            ScreenCapture capture = new _SCREEN_CAPTURE.ScreenCapture();
            Bitmap m = capture.StartCapture(false);
            if (m != null)
            {
                try
                {
                    BarcodeReader reader = new BarcodeReader();
                    reader.Options.CharacterSet = "UTF-8";
                    var result = reader.Decode(m);
                    TxtToResult.Text = (result == null) ? "" : result.Text;
                    pictureBox2.Image = m;
                }
                catch (Exception)
                {
                    Init();
                    MessageBox.Show("解析二维码失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                void Init()
                {
                    pictureBox2.Image = null; TxtToResult.Text = "";
                }
            }
        }

        private void TxtFromStr_DoubleClick(object sender, EventArgs e)
        {
            ryControls.TextBoxEx2 txt = (ryControls.TextBoxEx2)sender;
            开发辅助工具.Controls.FrmText frm = new Controls.FrmText
            {
                Icon = Icon
            };
            frm.richTextBox1.Text = txt.Text;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txt.Text = frm.richTextBox1.Text;
            }
        }
    }
}