using Azure.Cosmos;
using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Models;

namespace TEAMModeBI.Controllers
{
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [Route("common/login")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly IConfiguration _configuration;
        public LoginController(IConfiguration configuration)
        {
            _configuration = configuration;     
        }

       /// <summary>
       /// 钉钉扫描登录
       /// </summary>
       /// <param name="loginTmpCode"></param>
       /// <returns></returns>
        [ProducesDefaultResponseType]
        [HttpGet("dingding")]
        public IActionResult DingDingLogin(string loginTmpCode) 
        {
            string appKey = _configuration["appKey"];
            string appSecret = _configuration["appSecret"];
            string getuserinfo_bycode = _configuration["getuserinfo_bycode"];
            //判断参数是否为空
            if (string.IsNullOrEmpty(loginTmpCode))
            {
                return BadRequest("temp code error");
            }
            //获取access_token
            DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest request = new OapiGettokenRequest();
            request.Appkey = appKey;
            request.Appsecret = appSecret;
            request.SetHttpMethod("Get");
            OapiGettokenResponse response = client.Execute(request);
            if (response.IsError)
            {
                return BadRequest();
            }
            string access_token = response.AccessToken;
            //获取临时授权码 获取授权用户的个人信息
            DefaultDingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
            OapiSnsGetuserinfoBycodeRequest bycodeRequest = new OapiSnsGetuserinfoBycodeRequest()
            {
                //通过扫描二维码，跳转到指定的Url后，向Url中追加Code临时授权码
                TmpAuthCode = loginTmpCode
            };
            OapiSnsGetuserinfoBycodeResponse bycodeResponse = client1.Execute(bycodeRequest, appKey, appSecret);
            if (bycodeResponse.IsError)
            {
                return BadRequest();
            }
            //根据unionid获取userid
            string unionid = bycodeResponse.UserInfo.Unionid;
            DefaultDingTalkClient clientDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid");
            OapiUserGetbyunionidRequest byunionidRequest = new OapiUserGetbyunionidRequest()
            {
                Unionid = unionid
            };
            OapiUserGetbyunionidResponse byunionidResponse = clientDingTalkClient.Execute(byunionidRequest, access_token);
            if (byunionidResponse.IsError)
            {
                return BadRequest();
            }
            string userid = byunionidResponse.Result.Userid;
            //根据userId获取用户信息
            DefaultDingTalkClient clientDingTalkClient2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiV2UserGetRequest getRequest = new OapiV2UserGetRequest()
            {
                Userid = userid,
                Language="zh_CN"
            };
            getRequest.SetHttpMethod("Get");
            OapiV2UserGetResponse getResponse = clientDingTalkClient2.Execute(getRequest, access_token);
            if (getResponse.IsError) 
            {
                return BadRequest();
            }
            return Ok(getResponse);
        }

    }
}
