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.Models;
using LitJson;
using Library;

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




        #region 通用-获取小程序版本
        // [Authorize]
        public JsonResult GetCurrentVer(string value)
        {
            // value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            int ProjectId = int.Parse(function.CheckInt(data["ProjectId"].ToString())); //小程序项目
            string AppId = data["AppId"].ToString(); //平台分配的AppId
            string Token = data["Token"].ToString(); //用户令牌
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            if(function.MD5_32(AppId + "G9&E@952") != Token)
            {
                return Json(new AppResultJson() { Status = "-1", Info = "无权限", Data = Obj });
            }
            Users user = db.Users.FirstOrDefault(m => m.AppId == AppId) ?? new Users();
            bool check = db.ProjectVersion.Any(m => m.ProjectId == ProjectId && m.UserId == user.Id);
            if(!check)
            {
                return Json(new AppResultJson() { Status = "-1", Info = "暂无版本", Data = Obj });
            }
            ProjectVersion query = db.ProjectVersion.Where(m => m.ProjectId == ProjectId && m.UserId == user.Id).OrderByDescending(m => m.Id).FirstOrDefault() ?? new ProjectVersion();
            Obj.Add("VerNum", query.VerNum); //版本号
            Obj.Add("VerNo", query.VersionNo); //版本号
            Obj.Add("UpdateNote", query.UpdateNote); //更新说明
            Obj.Add("FilePath", SourceHost + query.FilePath); //模板文件路径
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        #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

    }
}
