using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace ChuPiao.AutoPrint.Machine.Printer
{
    ///   <summary>   
    ///   POSPrinter   的摘要说明。   
    ///   </summary>   
    public class POSPrinter
    {
        const int PrintNullLineCount = 3;
        const int OPEN_EXISTING = 3;
        string prnPort = "LPT1";
        /*
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr CreateFile(string lpFileName,
        int dwDesiredAccess,
        int dwShareMode,
        int lpSecurityAttributes,
        int dwCreationDisposition,
        int dwFlagsAndAttributes,
        int hTemplateFile);
        */

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern SafeFileHandle CreateFile(string lpFileName,
        int dwDesiredAccess,
        int dwShareMode,
        int lpSecurityAttributes,
        int dwCreationDisposition,
        int dwFlagsAndAttributes,
        int hTemplateFile);

        public POSPrinter()
        {
            //   
            //   TODO:   在此处添加构造函数逻辑   
            //   
        }
        public POSPrinter(string prnPort)
        {
            this.prnPort = prnPort;//打印机端口   
        }
        public string PrintLine(string str)
        {

            SafeFileHandle iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
            if (iHandle.IsInvalid == true)
            {
                return "没有连接打印机或者打印机端口不是LPT1";
            }
            else
            {
                FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);   //写数据   
                sw.WriteLine(str);
                //开钱箱       
                sw.WriteLine(ToCh(27) + ToCh(112) + "00");
                sw.Close();
                fs.Close();
                return "";
            }
        }

        /// <summary>
        ///打印信息
        /// </summary>
        /// <param name="MsgList">打印列表</param>
        /// <param name="isopenqx">是否打开钱箱</param>
        /// <returns></returns>
        public bool PrintMsg(List<string> MsgList, bool isopenqx)
        {
            bool rbool = false;
            SafeFileHandle iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
            if (iHandle.IsInvalid == true)
            {
                return rbool;
            }
            else
            {

                FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);   //写数据   
                //开钱箱   
                if (isopenqx)
                {
                    sw.WriteLine(ToCh(27) + ToCh(112) + "00");
                }
                //发送数据
                foreach (string str in MsgList)
                {
                    sw.WriteLine(str);
                }

                //发送空行
                for (int i = 0; i < PrintNullLineCount; i++)
                {
                    sw.WriteLine(ToCh(13));
                }

                sw.Close();
                fs.Close();
                rbool = true;
            }
            return rbool;
        }

        private string ToCh(int i)
        {
            return Convert.ToChar(i).ToString();
        }

        /// <summary>
        /// 打印Byte文件
        /// </summary>
        /// <param name="byteArr"></param>
        /// <returns></returns>
        public bool PrintByte(byte[] byteArr)
        {
            bool rbool = false;
            SafeFileHandle iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
            if (iHandle.IsInvalid == true)
            {
                return rbool;
            }
            else
            {
                FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
                //fs.Seek(0, SeekOrigin.Begin);
                fs.Write(byteArr, 0, byteArr.Length);
                fs.Close();
                rbool = true;
            }
            return rbool;
        }

        /// <summary>
        /// 打印彩票机
        /// </summary>
        /// <param name="InfoList"></param>
        /// <returns></returns>
        public bool PrintCpjInfo(List<string> InfoList)
        {

            bool rbool = false;
            SafeFileHandle iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
            if (iHandle.IsInvalid == true)
            {
                return rbool;
            }
            else
            {

                FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);   //写数据   

                foreach (string str in InfoList)
                {
                    sw.WriteLine(str);
                }
                sw.Close();
                fs.Close();
                rbool = true;
            }
            return rbool;
        }


    }
}
