using System;
using System.IO;
using System.Text;
using ChuPiao.Common.Models;

namespace ChuPiao.AutoPrint.Services.TicketFile
{
    public static class FileService
    {
        public const string DataRootPath=@"d:\data\logs\";
        public static string GetTicketFilePath(string macnumber,string ticketCode)
        {
            string ticketFilePath = Path.Combine(DataRootPath,macnumber);
            ticketFilePath = Path.Combine(ticketFilePath, DateTime.Now.ToString("yyyy-MM-dd"));
            ticketFilePath = Path.Combine(ticketFilePath, "ticket");
            if (!Directory.Exists(ticketFilePath))
            {
                Directory.CreateDirectory(ticketFilePath);
            }
            ticketFilePath = Path.Combine(ticketFilePath, ticketCode + ".prn");
            return ticketFilePath;
        }
        public static string GetErrorTicketFilePath(string macnumber, string ticketCode)
        {
            string ticketFilePath = Path.Combine(DataRootPath, macnumber);
            ticketFilePath = Path.Combine(ticketFilePath, DateTime.Now.ToString("yyyy-MM-dd"));
            ticketFilePath = Path.Combine(ticketFilePath, "errorticket");
            if (!Directory.Exists(ticketFilePath))
            {
                Directory.CreateDirectory(ticketFilePath);
            }
            ticketFilePath = Path.Combine(ticketFilePath, ticketCode + ".prn");
            return ticketFilePath;
        }
        public static void SavePrintInfoToLocalFile(L_LotTicketEntity ticketEntity, L_PrintTicketEntity printTicketEntity)
        {
            string ticketFilePath = Path.Combine(DataRootPath, ticketEntity.L_MacNumber);
            ticketFilePath = Path.Combine(ticketFilePath, "printinfo.txt");
            string msg = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}\t{13}\t{14}\t{15}\t{16}", ticketEntity.LotId, ticketEntity.L_TicketSn, ticketEntity.L_Id, ticketEntity.L_MacNumber, ticketEntity.L_Status, ticketEntity.L_LimitType, ticketEntity.L_NotifyStatus, printTicketEntity.P_Money, printTicketEntity.P_TicketCode, printTicketEntity.P_Password, printTicketEntity.P_RandomCode, printTicketEntity.P_SucTime.ToString("yyyy-MM-dd HH:mm:ss"), printTicketEntity.P_PrnText, printTicketEntity.P_SaleAddress, printTicketEntity.L_CodeRate, printTicketEntity.L_MaxGetMoney, printTicketEntity.L_CodeReference);
            File.WriteAllText(ticketFilePath, msg,Encoding.GetEncoding("gb2312"));
            
        }
        public static bool LoadPrintInfoFromLocalFile(string macnumber,L_LotTicketEntity ticketEntity, L_PrintTicketEntity printTicketEntity)
        {
            bool result = false;
            string ticketFilePath = Path.Combine(DataRootPath, macnumber);
            ticketFilePath = Path.Combine(ticketFilePath, "printinfo.txt");
            if(File.Exists(ticketFilePath))
            {
                string msg = File.ReadAllText(ticketFilePath, Encoding.GetEncoding("gb2312"));
                string[] arr = msg.Split('\t');
                if(arr.Length==16)
                {
                    ticketEntity.LotId = int.Parse(arr[0]);
                    ticketEntity.L_TicketSn = arr[1];
                    ticketEntity.L_Id = int.Parse(arr[2]);
                    ticketEntity.L_MacNumber = arr[3];
                    ticketEntity.L_Status = int.Parse(arr[4]);
                    ticketEntity.L_LimitType = int.Parse(arr[5]);
                    ticketEntity.L_NotifyStatus = int.Parse(arr[6]);

                    printTicketEntity.P_Money = decimal.Parse(arr[7]);
                    printTicketEntity.P_TicketCode = arr[8];
                    printTicketEntity.P_Password = arr[9];
                    printTicketEntity.P_RandomCode = arr[10];
                    printTicketEntity.P_SucTime = DateTime.Parse(arr[11]);
                    printTicketEntity.P_PrnText = arr[12];
                    printTicketEntity.P_SaleAddress = arr[13];
                    printTicketEntity.L_CodeRate = arr[14];
                    printTicketEntity.L_MaxGetMoney = decimal.Parse(arr[15]);
                    printTicketEntity.L_CodeReference = arr[16];

                    ticketEntity.L_Money = printTicketEntity.P_Money;
                    ticketEntity.L_TicketCode = printTicketEntity.P_TicketCode;
                    ticketEntity.L_Password = printTicketEntity.P_Password;
                    ticketEntity.L_RandomCode = printTicketEntity.P_RandomCode;
                    ticketEntity.L_CodeRate = printTicketEntity.L_CodeRate;
                    ticketEntity.L_CodeReference = printTicketEntity.L_CodeReference;

                    FileInfo fileInfo = new FileInfo(ticketFilePath);
                    string newticketFilePath = Path.Combine(DataRootPath, macnumber);
                    newticketFilePath = Path.Combine(newticketFilePath, DateTime.Now.ToString("yyyy-MM-dd"));
                    newticketFilePath = Path.Combine(newticketFilePath,
                                                     string.Format("printinfo_{0}.txt",
                                                                   DateTime.Now.ToString("yyyyMMddHHmmss")));
                    fileInfo.MoveTo(newticketFilePath);
                    result = true;
                }
            }
            return result;
        }
    }
}
