using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MySystem.MainModels;
using System.Linq;
using Library;
using LitJson;
using System;
using System.IO;

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 IActionResult Error()
        {
            string isapi = Request.Headers["Api"].ToString();
            if (isapi != "1")
            {
                if (Response.StatusCode == 500)
                {
                    return Redirect("/public/errpage/pc/500.html");
                }
                else if (Response.StatusCode == 502)
                {
                    return Redirect("/public/errpage/pc/502.html");
                }
                else if (Response.StatusCode == 404)
                {
                    return Redirect("/public/errpage/pc/404.html");
                }
                else if (Response.StatusCode == 401)
                {
                    return Content("401");
                }
            }
            return View();
        }

        public string auth(string app_id, string source, string application_type, string app_auth_code, string state)
        {
            return app_id + "\n" + source + "\n" + application_type + "\n" + app_auth_code + "\n" + state + "\n" + Request.QueryString.ToString();
        }

        public string test()
        {
            string result = "";
            // List<ReceiverList> Receivers = new List<ReceiverList>();
            // Receivers.Add(new ReceiverList()
            // {
            //     type = "MERCHANT_ID",  //分账接收方类型
            //     account = "1611167423",  //分账接收方账号
            //     amount = 1,  //分账金额
            //     description = "服务费",  //分账描述
            // });
            // Receivers.Add(new ReceiverList()
            // {
            //     type = "PERSONAL_OPENID",  //分账接收方类型
            //     account = "ooDpv5LkCKEguE-LMZO-rSV4TDpk",  //分账接收方账号
            //     amount = 1,  //分账金额
            //     description = "优惠",  //分账描述
            // });
            // result = new WeChatFunction().ProfitShare("1645266943", "4200001846202306021274142861", "2023060214014405449699901", Receivers);
            // result = new WeChatFunction().AddReceive("1645266943", "PERSONAL_OPENID", "ooDpv5LkCKEguE-LMZO-rSV4TDpk", "", "USER");
            // result = new WeChatFunction().QueryProfitShare("1645266943", "4200001815202306025662773463", "2023060213335030713572885");
            // result += new WeChatFunction().Unfreeze("1647917209","4200001851202306296256433756","2023062914544345583469577", "结算");
            // result += new WeChatFunction().Unfreeze("1647917209","4200001871202306297971809063","2023062914503970424085049", "结算");
            // result += new WeChatFunction().Unfreeze("1647917209","4200001861202306299303626271","2023062914274571909443048", "结算");

            // result = new WeChatFunction().ProfitShareReturn("1646405429", "1611167423", "2023060810550382756164803", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), 0.02M);

            // result = new WeChatFunction().GetPublicKey();

            return result;
        }

        public string makeCode()
        {
            string code = "";
            List<string> list = new List<string>();
            string start = "LKB0123090019999";
            for(int i = 0;i<=9999;i++)
            {
                string num = i.ToString();
                for(int j = 0;j< 4 - i.ToString().Length;j++)
                {
                    num = "0" + num;
                }
                string sn = dbconn.Encrypt3DES(start + num + function.get_Random(8), "l2k0b2#3");
                sn = sn.Substring(0, sn.Length - 1);
                code += System.Web.HttpUtility.UrlEncode(sn) + "\n";
            }
            return code;
        }
    }
}