using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Library;
using LitJson;

namespace MySystem.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public string test()
        {
            // testReq("http://idc.pay-api.top/idc-push-api/third/1050056/1560192687980675072/1", "bind_merchant");
            // testReq("http://idc.pay-api.top/idc-push-api/third/1050056/1560192687980675072/8", "un_bind_merchant");
            // testReq("http://idc.pay-api.top/idc-push-api/third/1050056/1560192687980675072/236", "change_bind_merchant");
            // testReq("http://idc.pay-api.top/idc-push-api/third/1050056/1560192687980675072/4", "sim_charge");
            testReq("http://idc.pay-api.top/idc-push-api/third/1050056/1560192687980675072/3", "service_fee");
            testReq("http://idc.pay-api.top/idc-push-api/third/1050056/1560192687980675072/2", "trade");
            return "ok";
        }

        public string testReq(string requrl, string reqkind)
        {
            SortedList<string, string> obj = new SortedList<string, string>();
            if(reqkind == "bind_merchant") obj = bind_merchant();
            if(reqkind == "un_bind_merchant") obj = un_bind_merchant();
            if(reqkind == "change_bind_merchant") obj = change_bind_merchant();
            if(reqkind == "sim_charge") obj = sim_charge();
            if(reqkind == "service_fee") obj = service_fee();
            if(reqkind == "trade") obj = trade();
            string content = EncryptHelper.Encrypt1(obj, "57yJ3T1Btz7LytyL");
            obj = new SortedList<string, string>();
            obj.Add("type", reqkind);
            obj.Add("app_id", "KXS000001");
            obj.Add("notice_id", Guid.NewGuid().ToString());
            obj.Add("timestamp", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8));
            obj.Add("content", content);
            string requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            function.WriteLog("请求地址:" + requrl);
            function.WriteLog("请求参数:" + requestJson);
            string result = function.PostWebRequest(requrl, requestJson, "application/json");
            function.WriteLog("返回:" + result);
            function.WriteLog("\n\n\n");
            return result;
        }

        private SortedList<string, string> bind_merchant()
        {
            SortedList<string, string> obj = new SortedList<string, string>();
            obj.Add("phone", "**********");
            obj.Add("name", "杨洪江");
            obj.Add("supplement_info", "533022********2015");
            obj.Add("agent_id", "039034");
            obj.Add("merch_no", "039331007177057");
            obj.Add("pos_sn", "00005702280118855803");
            obj.Add("device_model", "盒易付H版");
            obj.Add("term_to", "00005702280118855803");
            obj.Add("bind_time", "2023-12-14 16:32:38");
            return obj;
        }

        private SortedList<string, string> un_bind_merchant()
        {
            SortedList<string, string> obj = new SortedList<string, string>();
            obj.Add("name", "张**");
            obj.Add("supplement_info", "4307************48");
            obj.Add("agent_id", "88880019");
            obj.Add("merch_no", "LKB50009993529999");
            obj.Add("pos_sn", "LKB1888888");
            obj.Add("device_model", "N101");
            obj.Add("bind_time", "2019-03-12 22:21:50");
            return obj;
        }

        private SortedList<string, string> change_bind_merchant()
        {
            SortedList<string, string> obj = new SortedList<string, string>();
            obj.Add("name", "张**");
            obj.Add("supplement_info", "4307************48");
            obj.Add("agent_id", "88880019");
            obj.Add("merch_no", "LKB50009993529999");
            obj.Add("before_pos_sn", "LKB1888888");
            obj.Add("after_pos_sn", "LKB1888888");
            obj.Add("before_device_model", "N101");
            obj.Add("after_device_model", "N102");
            obj.Add("bind_time", "2019-03-12 22:21:50");
            return obj;
        }

        private SortedList<string, string> sim_charge()
        {
            SortedList<string, string> obj = new SortedList<string, string>();
            obj.Add("merch_no", "LKB50009993529999");
            obj.Add("agent_id", "88880019");
            obj.Add("pos_sn", "LKB188FBMS78888");
            obj.Add("amount", "9900");
            obj.Add("order_id", "2004190320190312222130116680");
            obj.Add("trans_time", "2019-03-12 22:23:02");
            return obj;
        }

        private SortedList<string, string> service_fee()
        {
            SortedList<string, string> obj = new SortedList<string, string>();
            obj.Add("merch_no", "039331007177057");
            obj.Add("agent_id", "039034");
            obj.Add("pos_sn", "00005702280118855803");
            obj.Add("order_id", "20231214H00040601976Y");
            obj.Add("amount", "29900");
            obj.Add("trans_time", "2023-12-14 16:39:43");
            return obj;
        }

        private SortedList<string, string> trade()
        {
            SortedList<string, string> obj = new SortedList<string, string>();
            obj.Add("agent_id", "039034");
            obj.Add("order_id", "20231214H00040608996Y");
            obj.Add("trans_id", "20231214H00040608996Y");
            obj.Add("merch_no", "039331007177057");
            obj.Add("term_no", "00005702280118855803");
            obj.Add("pos_sn", "00005702280118855803");
            obj.Add("amount", "102500");
            obj.Add("trans_time", "2023-12-14 16:54:17");
            obj.Add("ret_code", "00");
            obj.Add("ret_msg", "交易成功");
            obj.Add("fee_amount", "615");
            obj.Add("pay_type", "0");
            obj.Add("card_type", "C");
            obj.Add("trans_type", "31");
            return obj;
        }

        public string push()
        {
            // AliyunPushHelper.Instance.DoSomeThing("{\"Account\":\"14726006947\",\"Device\":\"ANDROID\",\"Title\":\"测试推送\",\"Body\":\"客小爽推送功能即将上线\"}");
            // AliyunPushHelper.Instance.DoSomeThing("{\"Account\":\"14726006947\",\"Device\":\"IOS\",\"Title\":\"测试推送\",\"Body\":\"客小爽推送功能即将上线\"}");
            // string content = "{\"data_type\":\"bind\",\"data_content\":{\"pos_sn\":\"1014BS0023511\",\"mer_no\":\"15608798003\",\"id_card\":null,\"mer_name\":\"15608798003\",\"mer_mobile\":\"15608798003\",\"request_id\":\"2024102310381200098289115\",\"bind_time\":\"2024-10-23 10:38:12\",\"brand\":\"23\"}}";
            // PosPushHelper.Instance.DoSomeThing(content);
            // SortedList<string, string> obj = new SortedList<string, string>();
            // obj.Add("pos_sn", "LDN7HDQM365548961135");
            // obj.Add("merch_no", "015330849562900");
            // obj.Add("agent_id", "0000001");
            // obj.Add("supplement_info", "");
            // obj.Add("name", "时代电器经营部");
            // obj.Add("phone", "");
            // obj.Add("request_id", "ac76801a7b0773063ac20ece3f8ca6dc");
            // obj.Add("device_model", "000002");
            // obj.Add("term_to", "");
            // obj.Add("bind_time", "2024-12-27 10:16:10");
            // jm(obj, "bind");

            SortedList<string, string> obj = new SortedList<string, string>();
            obj.Add("agent_id","0000001");
            obj.Add("busi_type","");
            obj.Add("busi_sub_type","");
            obj.Add("order_id","G1241227C03142143729");
            obj.Add("trans_id","G1241227C03142143729");
            obj.Add("merch_no","015330849562900");
            obj.Add("term_no","");
            obj.Add("pos_sn","LDN7HDQM365548961135");
            obj.Add("refer_no","");
            obj.Add("card_no","");
            obj.Add("amount","48.00");
            obj.Add("trans_time","2024-12-27 00:00:27");
            obj.Add("longitude","0");
            obj.Add("latitude","0");
            obj.Add("ret_code","00");
            obj.Add("ret_msg","交易成功");
            obj.Add("area_code","");
            obj.Add("fee_amount","0");
            obj.Add("pay_type","");
            jm(obj, "trade");

            return "ok";
        }

        public string jm(SortedList<string, string> obj, string type)
        {
            string NoticeUrl = "https://sy.sic-credit.com:9091/prod-api/space/open/api/commerce/offline/business/data";
            string PushData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            LogHelper.Instance.WriteLog("原始数据：" + PushData, "推送数据日志");
            string content = EncryptHelper.Encrypt1(obj, "KTVpzn70n3Bv7w04");
            LogHelper.Instance.WriteLog("加密数据：" + content, "推送数据日志");
            obj = new SortedList<string, string>();
            obj.Add("type", type);
            obj.Add("notice_id", Guid.NewGuid().ToString());
            obj.Add("timestamp", function.getTimeStamp());
            obj.Add("content", content);
            string requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            string PushDataEncrypt = requestJson;
            LogHelper.Instance.WriteLog("请求参数：" + PushDataEncrypt, "推送数据日志");
            LogHelper.Instance.WriteLog("请求地址：" + NoticeUrl, "推送数据日志");
            string result = function.PostWebRequest(NoticeUrl, requestJson, "application/json");
            LogHelper.Instance.WriteLog("返回报文：" + result + "\n\n", "推送数据日志");
            
            return result;
        }

        public string push2()
        {
            // List<string> list = new List<string>();
            string[] list = function.ReadInstance("9.txt").Split('\n');
            string result = ",";
            foreach(string sub in list)
            {
                function.WriteLog(sub);
                string[] data = sub.Split('|');
                result += function.GetWebRequest("https://logic-executor-api.kexiaoshuang.com/noauth/prize/pushbyone?n=" + data[1] + "&m=" + data[0] + "&ids=" + data[2]) + ",";
            }
            return result;
        }
    }
}