using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using System.Web;
using MySystem.MainModels;
using LitJson;
using Library;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("Api/v1/[controller]/[action]")]
    public class KqProductsController : BaseController
    {
        public KqProductsController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }




        #region 通用-产品列表
        [Authorize]
        public JsonResult List(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = ListDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> ListDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            Dictionary<string, object> curData = new Dictionary<string, object>();
            curData.Add("Name", "微信"); //名称
            curData.Add("Id", "2"); //Id
            dataList.Add(curData);

            curData = new Dictionary<string, object>();
            curData.Add("Name", "支付宝"); //名称
            curData.Add("Id", "1"); //Id
            dataList.Add(curData);
            return dataList;
        }
        #endregion


        #region 首页-客小爽产品-主界面产品数据
        [Authorize]
        public JsonResult MainStatData(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> Obj = MainStatDataDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public List<Dictionary<string, object>> MainStatDataDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string UserId = data["UserId"].ToString(); //创客Id
            
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            Dictionary<string, object> Obj = new Dictionary<string, object>();

            UserMachineData MachineData = UserMachineDataDbconn.Instance.Get(UserId + "_0") ?? new UserMachineData();
            UserMachineData MachineData2 = UserMachineDataDbconn.Instance.Get(UserId + "_1") ?? new UserMachineData();
            Dictionary<string, object> Machine = new Dictionary<string, object>();
            Dictionary<string, object> Machine2 = new Dictionary<string, object>();
            Machine.Add("TotalMachineCount", MachineData.TotalMachineCount);  //总机具数
            Machine.Add("UnBindCount", MachineData.UnBindCount); //未绑定机具数
            Machine.Add("BindCount", MachineData.BindCount); //已绑定机具数
            Obj.Add("Machine", Machine); //机具管理数据

            Machine2.Add("TotalMachineCount2", MachineData2.TotalMachineCount);  //总机具数
            Machine2.Add("UnBindCount2", MachineData2.UnBindCount); //未绑定机具数
            Machine2.Add("BindCount2", MachineData2.BindCount); //已绑定机具数
            Obj.Add("Machine2", Machine2); //机具管理数据

            Dictionary<string, object> Profit = new Dictionary<string, object>();
            Profit.Add("TradeProfit", MachineData.TradeProfit); //交易分润
            Profit.Add("ActProfit", MachineData.ActProfit); //激活奖励
            Profit.Add("OpenProfit", MachineData.OpenProfit); //开机奖励
            Profit.Add("DividendsProfit", MachineData.DividendsProfit); //分红奖励
            Profit.Add("OtherProfit", MachineData.OtherProfit); //分润补贴
            Profit.Add("FluxProfit", MachineData.FluxProfit); //流量卡分佣
            Obj.Add("Profit", Profit); //收益查看数据

            List<Dictionary<string, object>> Advertisment = new List<Dictionary<string, object>>();
            string ColId = "";
            List<Models.Advertisment> ads = AdvertismentDbconn.Instance.GetList(ColId);
            foreach (Models.Advertisment ad in ads)
            {
                Dictionary<string, object> item = new Dictionary<string, object>();
                item.Add("Url", ad.Url); //跳转地址
                item.Add("BannerPic", DefaultPic(ad.PicPath)); //广告图片
                item.Add("Id", ad.Id); //Id
                Advertisment.Add(item);
            }
            Obj.Add("Advertisment", Advertisment);
            list.Add(Obj);
            return list;
        }
        #endregion





        #region 检查签名是否合法，合法返回1，不合法返回提示信息

        /// <summary>
        /// 检查签名是否合法，合法返回1，不合法返回提示信息
        /// </summary>
        /// <param name="value">请求的参数(json字符串)</param>
        /// <param name="signField">要签名的字段</param>
        /// <returns></returns>
        private string CheckSign(string value, string[] signField)
        {
            JsonData json = JsonMapper.ToObject(value);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            for (int i = 0; i < signField.Length; i++)
            {
                dic.Add(signField[i], json[signField[i]].ToString());
            }
            string sign = json["sign"].ToString(); //客户端签名字符串
            return new Sign().sign(dic, sign);
        }

        #endregion

    }
}
