
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Management;
using tool;
using System.Drawing;
using System.Speech;
using System.Speech.Synthesis;

namespace tool
{
    public class PrintManual : Singleton<PrintManual>
    {
        private PrintDocument m_print_document = new PrintDocument();    //获取默认打印机的方法
        private Image m_image = null;
        private string m_code_data = "";
        private string m_tip_data = "";

        private PrintManual()
        {
        }
        /// <summary>
        /// 获取二维码图片
        /// </summary>
        /// <returns></returns>

        public Image GetQRcode(string data)
        {
            Image qrcode = QRcodeGenerator.Encode(data);
            return qrcode;
        }


        /// <summary>
        /// 打印二维码(外部调用)
        /// </summary>
        /// <param name="code_data">二维码内容</param>
        /// <param name="tip_data">提示内容</param>
        public void PrintTicket(string code_data, string tip_data)
        {
            //生成二维码图片
            m_image = QRcodeGenerator.Encode(code_data);
            m_code_data = code_data;
            m_tip_data = tip_data;
            //打印
            if (this.Print())
            {
                this.speak("停车成功，请取走您的小票！");
                Console.WriteLine("打印成功！");
            }
        }
        /// <summary>
        /// 打印（内部生成二维码后调用调用）
        /// </summary>
        /// <param name="_img">二维码图片</param>
        /// <param name="code_data"></param>
        /// <param name="tip_data"></param>
        private bool Print()
        {
            //Console.WriteLine($"PaperName:{m_print_document.PrinterSettings.PrinterName}, PaperSize: {m_print_document.PrinterSettings}");
            //float m_pageWidth = 30;//纸张宽度 mm单位
            //float m_pageHeight = 30;//纸张高度 mm单位
            //单位？？？？
            //m_print_document.DefaultPageSettings.PaperSize = new PaperSize("newPage70X40", (int)(m_pageWidth / 25.4 * 100), (int)(m_pageHeight / 25.4 * 100));
            //自定义图片内容整体上间距/左间距
            m_print_document.OriginAtMargins = false;
            //m_print_document.DefaultPageSettings.Margins.Top = (int)(0.1 / 25.4 * 100);
            //m_print_document.DefaultPageSettings.Margins.Left = (int)(0.1 / 25.4 * 100);
            //实现PrintPageEventHandler委托
            m_print_document.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
            try
            {
                m_print_document.Print();
                m_image = null;
                m_code_data = "";
                m_tip_data = "";
                return true;
            }
            catch (Exception ex)
            {
                m_print_document.PrintController.OnEndPrint(m_print_document, new PrintEventArgs());
                return false;
            }
        }
        /// <summary>
        /// 打印页面设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int x = (m_print_document.DefaultPageSettings.PaperSize.Width - m_image.Width) / 2  -6;
            int y = m_image.Height;
            Font font = new Font("宋体", 9);
            e.Graphics.DrawImage(m_image, x, -30);//二维码中已经存储取车信息
            e.Graphics.DrawString("      车牌号：" + m_tip_data, font, Brushes.Black, x, y-25);
            e.Graphics.DrawString(" 请妥善保存取车码,丢失需补办！", font, Brushes.Black, x, y-5);//提示信息
            e.Graphics.DrawString("      "+DateTime.Now.ToString(), font, Brushes.Black, x, y+15);//时间
            font = new Font("宋体", 10, FontStyle.Bold);
            e.Graphics.DrawString(".", font, Brushes.Black, x, y+30);
        }
        /// <summary>
        /// 语音提示
        /// </summary>
        /// <param name="APIType">要获取的类型的 ProgID</param>
        /// <param name="content">语音内容</param>
        private void speak(string content)
        {
            Task.Factory.StartNew(() =>
            {
                SpeechSynthesizer speech = new SpeechSynthesizer();
                try
                {
                    speech.Speak(content);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"报错:{ex?.Message}");
                }
            });
        }

    }
}
