using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Common.API;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Edu.Common.Plugin;
using Aliyun.Acs.vod.Model.V20170321;
using Aliyun.Net.SDK.Core;
using Aliyun.Acs.Core.Profile;

namespace Edu.WebApi.Controllers.Video
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class VideoController : BaseController
    {


        /// <summary>
        /// 获取视频播放凭证
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetVideoPlayAuth()
        {
            JObject jobj = JObject.Parse(RequestParm.Msg.ToString());
            string VideoId = jobj.GetStringValue("strVid");

            try
            {
                string regionId = "cn-shanghai";
                IClientProfile profile = DefaultProfile.GetProfile(regionId, "LTAI5tBoku6yPFSkacA3sbAT", "zcjzTpMGyPwchaKAyUFeKNEBcqAxvL");
                var acsClient = new Aliyun.Acs.Core.DefaultAcsClient(profile);

                if (string.IsNullOrEmpty(VideoId))
                {
                    return ApiResult.Failed("获取失败");
                }
                GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
                request.VideoId = VideoId;
                request.AuthInfoTimeout = 30000;
                // 初始化客户端
                // DefaultAcsClient client = InitVodClient();
                // 发起请求，并得到 response

                GetVideoPlayAuthResponse response = acsClient.GetAcsResponse(request);
                GetVideoPlayAuthResponse.GetVideoPlayAuth_VideoMeta VideoMetaModel = response.VideoMeta;
                string RequestId = response.RequestId;
                string PlayAuth = response.PlayAuth;
                if (response != null && !string.IsNullOrWhiteSpace(response.RequestId))
                {
                    return ApiResult.Success("", new { RequestId, PlayAuth, VideoMeta= VideoMetaModel });
                }
                else
                {
                    return ApiResult.Failed("获取失败");
                }

            }
            catch (Exception)
            {
                return ApiResult.Failed("获取失败");
            }
        }




        /// <summary>
        /// 获取播放地址
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetPlayInfo()
        {
            JObject jobj = JObject.Parse(RequestParm.Msg.ToString());
            string VideoId = jobj.GetStringValue("strVid");

            try
            {
                string regionId = "cn-shanghai";
                IClientProfile profile = DefaultProfile.GetProfile(regionId, "LTAI5tBoku6yPFSkacA3sbAT", "zcjzTpMGyPwchaKAyUFeKNEBcqAxvL");
                var acsClient = new Aliyun.Acs.Core.DefaultAcsClient(profile);

                if (string.IsNullOrEmpty(VideoId))
                {
                    return ApiResult.Failed("获取失败");
                }
                GetPlayInfoRequest request = new GetPlayInfoRequest();
                request.VideoId = VideoId;
                // 初始化客户端
                // DefaultAcsClient client = InitVodClient();
                // 发起请求，并得到 response

                GetPlayInfoResponse response = acsClient.GetAcsResponse(request);
                List<GetPlayInfoResponse.GetPlayInfo_PlayInfo> playInfoList = response.PlayInfoList;
                GetPlayInfoResponse.GetPlayInfo_VideoBase videoBase = response.VideoBase;
                if (playInfoList.Count > 0)
                {
                    return ApiResult.Success("",new { URL= playInfoList[0].PlayURL ,VideoImg= videoBase.CoverURL});
                }
                else
                {
                    return ApiResult.Failed("获取失败");
                }
            }
            catch (Exception)
            {
                return ApiResult.Failed("获取失败");
            }
        }
    }
}
