using Common.Request;
using Common.Request.Electronic;
using Common.Request.Label;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Utils;
using static System.Net.WebRequestMethods;
using Common.Request.cloud;

namespace Demo
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args == null || args.Length < 2) return;
            string rFlag = args[0];
            string rparam = args[1];
            string rBody = Base64Helper.DecodeBase64(rparam);
            if (string.Compare(rFlag, "order") == 0)
            {
                OrderParam op = new OrderParam();
                ImputOrderParam(rBody, ref op);
                string order = API.GeneLabelOrder(op);
                WriteTxt(order, "order.txt");
            }
            else
            if (string.Compare(rFlag, "printOld") == 0)
            {
                PrintOldParam po = new PrintOldParam();
                po = JsonConvert.DeserializeObject<PrintOldParam>(rBody);
                string printOld = API.CloudPrintOld(po);
                WriteTxt(printOld, "printOld.txt");
            }

        }

        #region 初始化OrderParam变量
        private static void ImputOrderParam(string rbody, ref OrderParam op)
        {
            op = JsonConvert.DeserializeObject<OrderParam>(rbody);
        }
        #endregion


        #region 将字符串写入txt文档
        private static void WriteTxt(string rIOParam,string rFileName)
        {
            string rFilePath = System.Environment.CurrentDirectory + "\\"+ rFileName;
            FileStream fs = new FileStream(rFilePath, FileMode.Create);
            byte[] data = Encoding.Default.GetBytes(rIOParam);
            try
            {
                fs.Write(data, 0, data.Length);
                fs.Flush();
                fs.Close();
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
    }
}

