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 System.Globalization;
using System.Runtime.InteropServices;

namespace OPOS_POSPrinter_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cmbDeviceName.Items.Add("FP1100LANPRT_192.168.10.210");
            cmbDeviceName.Items.Add("FP1100USBPRT");
            cmbDeviceName.Items.Add("FP510IILANPRT_192.168.10.210");
            cmbDeviceName.Items.Add("FP510IIUSBPRT");
            cmbDeviceName.SelectedIndex = 0;
        }

        private void btnDeviceOpen_Click(object sender, EventArgs e)
        {
            int iRet;

            try
            {
                //iRet = axFPPrinter1.Open(cmbDeviceName.SelectedItem.ToString());
                iRet = axFPPrinter1.Open(cmbDeviceName.Text);
                if (iRet != 0)
                {
                    MessageBox.Show("Open failed:" + iRet.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                if (iRet == 0)
                {
                    iRet = axFPPrinter1.Claim(1000);
                    if (iRet != 0)
                    {
                        MessageBox.Show("Claim failed:" + iRet.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                if (iRet == 0)
                {
                    axFPPrinter1.DeviceEnabled = true;
                    if (axFPPrinter1.ResultCode != 0)
                    {
                        MessageBox.Show("DeviceEnabled=True failed:" + axFPPrinter1.ResultCode.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void btnDeviceClose_Click(object sender, EventArgs e)
        {
            int iRet = 0;

            try
            {
                axFPPrinter1.DeviceEnabled = false;
                if (axFPPrinter1.ResultCode != 0)
                {
                    iRet = axFPPrinter1.ResultCode;
                    MessageBox.Show("DeviceEnabled=False failed:" + axFPPrinter1.ResultCode.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                if (iRet == 0)
                {
                    iRet = axFPPrinter1.Release();
                    if (iRet != 0)
                    {
                        MessageBox.Show("Release failed:" + iRet.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                if (iRet == 0)
                {
                    iRet = axFPPrinter1.Close();
                    if (iRet != 0)
                    {
                        MessageBox.Show("Close failed:" + iRet.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                // <Print Vietnamese>
                // You need to change "Region and Language" to Vietnamese.
                // 1. Control panel --> select "Region and Language" --> select "Formats" tab 
                //                  --> change the "Format:" to Vietnamese.
                // 2. Control panel --> select "Region and Language" --> select "Administrative" tab 
                //                  --> click [Change system locale...] --> change to Vietnamese.

                // Change printer code page to Vietnamese(1258)
                axFPPrinter1.CharacterSet = 1258;

                string  strUnicode  = "  ! \" # $ % & ' ( ) * + , - . / \x0a";
                        strUnicode += "0 1 2 3 4 5 6 7 8 9 : ; < = > ? \x0a";
                        strUnicode += "@ A B C D E F G H I J K L M N O \x0a";
                        strUnicode += "P Q R S T U V W X Y Z [ \\ ] ^ _ \x0a";
                        strUnicode += "` a b c d e f g h i j k l m n o \x0a";
                        strUnicode += "p q r s t u v w x y z { | } ~ \x0a";
                        strUnicode += "À Á Â Ã È É Ê Ì Í \x0a";
                        strUnicode += "Ò Ó Ô Õ Ù Ú Ý \x0a";
                        strUnicode += "à á â ã è é ê ì í \x0a";
                        strUnicode += "ò ó ô õ ù ú ý \x0a";
                        strUnicode += "Ă ă Đ đ Ĩ ĩ Ũ ũ \x0a";
                        strUnicode += "Ơ ơ Ư ư\x0a";
                        strUnicode += "A a Ả ả Ấ ấ Ầ ầ Ẩ ẩ Ẫ ẫ Ậ ậ Ắ ắ \x0a";
                        strUnicode += "Ằ ằ Ẳ ẳ Ẵ ẵ Ặ ặ Ẹ ẹ Ẻ ẻ Ẽ ẽ Ế ế \x0a";
                        strUnicode += "Ề ề Ể ể Ễ ễ Ệ ệ Ỉ ỉ Ị ị Ọ ọ Ỏ ỏ \x0a";
                        strUnicode += "Ố ố Ồ ồ Ổ ổ Ỗ ỗ Ộ ộ Ớ ớ Ờ ờ Ở ở \x0a";
                        strUnicode += "Ỡ ỡ Ợ ợ Ụ ụ Ủ ủ Ứ ứ Ừ ừ Ử ử Ữ ữ \x0a";
                        strUnicode += "Ự ự Ỳ ỳ Ỵ ỵ Ỷ ỷ Ỹ ỹ \x0a";

                // Change Unicode to printer code(TCVN5712-1993VN1)
                string strVietNam = ConvertToVietnam(strUnicode);

                // Print Vietnamese
                axFPPrinter1.PrintNormal(2, strVietNam);
          
                // Paper feed and cut
                axFPPrinter1.PrintNormal(2, "\x1B|100fP");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }


        public Dictionary<char, byte> _dicVietnam = new Dictionary<char, byte>()
        {
            {'Ú', 0x01},
            {'Ụ', 0x02},
            {'Ừ', 0x04},
            {'Ử', 0x05},
            {'Ữ', 0x06},
            {'Ứ', 0x11},
            {'Ự', 0x12},
            {'Ỳ', 0x13},
            {'Ỷ', 0x14},
            {'Ỹ', 0x15},
            {'Ý', 0x16},
            {'Ỵ', 0x17},
            {'À', 0x80},
            {'Ả', 0x81},
            {'Ã', 0x82},
            {'Á', 0x83},
            {'Ạ', 0x84},
            {'Ặ', 0x85},
            {'Ậ', 0x86},
            {'È', 0x87},
            {'Ẻ', 0x88},
            {'Ẽ', 0x89},
            {'É', 0x8A},
            {'Ẹ', 0x8B},
            {'Ệ', 0x8C},
            {'Ì', 0x8D},
            {'Ỉ', 0x8E},
            {'Ĩ', 0x8F},
            {'Í', 0x90},
            {'Ị', 0x91},
            {'Ò', 0x92},
            {'Ỏ', 0x93},
            {'Õ', 0x94},
            {'Ó', 0x95},
            {'Ọ', 0x96},
            {'Ộ', 0x97},
            {'Ờ', 0x98},
            {'Ở', 0x99},
            {'Ỡ', 0x9A},
            {'Ớ', 0x9B},
            {'Ợ', 0x9C},
            {'Ù', 0x9D},
            {'Ủ', 0x9E},
            {'Ũ', 0x9F},
            {'Ă', 0xA1},
            {'Â', 0xA2},
            {'Ê', 0xA3},
            {'Ô', 0xA4},
            {'Ơ', 0xA5},
            {'Ư', 0xA6},
            {'Đ', 0xA7},
            {'ă', 0xA8},
            {'â', 0xA9},
            {'ê', 0xAA},
            {'ô', 0xAB},
            {'ơ', 0xAC},
            {'ư', 0xAD},
            {'đ', 0xAE},
            {'Ằ', 0xAF},
            {'à', 0xB5},
            {'ả', 0xB6},
            {'ã', 0xB7},
            {'á', 0xB8},
            {'ạ', 0xB9},
            {'Ẳ', 0xBA},
            {'ằ', 0xBB},
            {'ẳ', 0xBC},
            {'ẵ', 0xBD},
            {'ắ', 0xBE},
            {'Ẵ', 0xBF},
            {'Ắ', 0xC0},
            {'Ầ', 0xC1},
            {'Ẩ', 0xC2},
            {'Ẫ', 0xC3},
            {'Ấ', 0xC4},
            {'Ề', 0xC5},
            {'ặ', 0xC6},
            {'ầ', 0xC7},
            {'ẩ', 0xC8},
            {'ẫ', 0xC9},
            {'ấ', 0xCA},
            {'ậ', 0xCB},
            {'è', 0xCC},
            {'Ể', 0xCD},
            {'ẻ', 0xCE},
            {'ẽ', 0xCF},
            {'é', 0xD0},
            {'ẹ', 0xD1},
            {'ề', 0xD2},
            {'ể', 0xD3},
            {'ễ', 0xD4},
            {'ế', 0xD5},
            {'ệ', 0xD6},
            {'ì', 0xD7},
            {'ỉ', 0xD8},
            {'Ễ', 0xD9},
            {'Ế', 0xDA},
            {'Ồ', 0xDB},
            {'ĩ', 0xDC},
            {'í', 0xDD},
            {'ị', 0xDE},
            {'ò', 0xDF},
            {'Ổ', 0xE0},
            {'ỏ', 0xE1},
            {'õ', 0xE2},
            {'ó', 0xE3},
            {'ọ', 0xE4},
            {'ồ', 0xE5},
            {'ổ', 0xE6},
            {'ỗ', 0xE7},
            {'ố', 0xE8},
            {'ộ', 0xE9},
            {'ờ', 0xEA},
            {'ở', 0xEB},
            {'ỡ', 0xEC},
            {'ớ', 0xED},
            {'ợ', 0xEE},
            {'ù', 0xEF},
            {'Ỗ', 0xF0},
            {'ủ', 0xF1},
            {'ũ', 0xF2},
            {'ú', 0xF3},
            {'ụ', 0xF4},
            {'ừ', 0xF5},
            {'ử', 0xF6},
            {'ữ', 0xF7},
            {'ứ', 0xF8},
            {'ự', 0xF9},
            {'ỳ', 0xFA},
            {'ỷ', 0xFB},
            {'ỹ', 0xFC},
            {'ý', 0xFD},
            {'ỵ', 0xFE},
            {'Ố', 0xFF}
        };


        private readonly Encoding _encVietnam = Encoding.GetEncoding(new CultureInfo("vi-VN").TextInfo.ANSICodePage);

        private string ConvertToVietnam(string line)
        {
            if (string.IsNullOrEmpty(line))
            {
                return line;
            }

            StringBuilder stb = new StringBuilder();

            foreach (var c in line)
            {
                byte b;
                if (this._dicVietnam.TryGetValue(c, out b))
                {
                    string s = this._encVietnam.GetString(new byte[] { b });
                    stb.Append(s);
                }
                else
                {
                    stb.Append(c);
                }
            }

            return stb.ToString();
        }


    }
}
