using CloudGaming.Api.Base;
using CloudGaming.Code.Contract;
using CloudGaming.Code.DataAccess;
using CloudGaming.Code.Game;
using CloudGaming.DtoModel.Account.User;
using CloudGaming.DtoModel.Game;
using CloudGaming.DtoModel.JY;
using CloudGaming.DtoModel.PlayGame;
using CloudGaming.Model.DbSqlServer.Db_Phone;

using HuanMeng.DotNetCore.Base;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using Newtonsoft.Json;


namespace CloudGaming.Api.Controllers;

/// <summary>
/// 玩游戏接口
/// </summary>
public class PlayGameController : CloudGamingControllerBase
{
    private IJYApi JYApi;
    public PlayGameController(IServiceProvider _serviceProvider, IJYApi jYApi) : base(_serviceProvider)
    {
        JYApi = jYApi;
    }

    /// <summary>
    /// 获取token接口
    /// </summary>
    /// <param name="requestBaseModel"></param>
    /// <returns></returns>
    [HttpPost]
    [Authorize]
    public async Task<BaseResponse<GetTokenModel>> GetTokenAsync([FromBody] RequestBaseModel requestBaseModel)
    {

        PlayGameBLL playGameBLL = new PlayGameBLL(ServiceProvider, JYApi);
        return await playGameBLL.GetTokenAsync(requestBaseModel);
    }

    /// <summary>
    /// 开始游戏
    /// </summary>
    /// <param name="playGameRequest"></param>
    /// <returns></returns>
    [HttpPost]
    [Authorize]
    public async Task<BaseResponse<dynamic>> PlayGameAsync([FromBody] PlayGameRequest playGameRequest)
    {
        PlayGameBLL playGameBLL = new PlayGameBLL(ServiceProvider, JYApi);
        return await playGameBLL.PlayGameAsync(playGameRequest);

    }

    /// <summary>
    /// 游戏心跳
    /// </summary>
    /// <param name="gameRequest"></param>
    /// <returns></returns>
    [HttpPost]
    [Authorize]
    public async Task<PlayGameHeartbeatResponse> PlayGameHeartbeatAsync([FromBody] GameRequest gameRequest)
    {
        PlayGameBLL playGameBLL = new PlayGameBLL(ServiceProvider, JYApi);
        return await playGameBLL.PlayGameHeartbeat(gameRequest.GameId);
    }

    /// <summary>
    /// 退出游戏
    /// </summary>
    /// <param name="gameRequest"></param>
    /// <returns></returns>
    [HttpPost]
    [Authorize]
    public async Task<BaseResponse<bool>> ExitPlayGame([FromBody] ExitPlayGameRequest gameRequest)
    {
        PlayGameBLL playGameBLL = new PlayGameBLL(ServiceProvider, JYApi);
        return await playGameBLL.ExitPlayGame(gameRequest);
    }




    /// <summary>
    /// 游戏排队
    /// </summary>
    /// <param name="gameRequest"></param>
    /// <returns></returns>
    [HttpGet]
    [Authorize]
    public async Task<BaseResponse<PlayQueueInfoModel>> PlayGameQueueAsync([FromQuery] GameRequest gameRequest)
    {
        PlayGameBLL playGameBLL = new PlayGameBLL(ServiceProvider, JYApi);
        return await playGameBLL.PlayGameQueueAsync(gameRequest);
    }

    /// <summary>
    /// 取消排队
    /// </summary>
    /// <param name="gameRequest"></param>
    /// <returns></returns>
    [HttpPost]
    [Authorize]
    public async Task<BaseResponse<bool>> CancelQueueAsync([FromBody] GameRequest gameRequest)
    {
        PlayGameBLL playGameBLL = new PlayGameBLL(ServiceProvider, JYApi);
        return await playGameBLL.CancelQueueAsync(gameRequest);
    }

    /// <summary>
    /// 切换视频等级
    /// </summary>
    /// <param name="displayGradeRequest"></param>
    /// <returns></returns>
    [HttpPost]
    [Authorize]
    public async Task<BaseResponse<object>> DisplayGradeAsync([FromBody] DisplayGradeRequest displayGradeRequest)
    {

        PlayGameBLL playGameBLL = new PlayGameBLL(ServiceProvider, JYApi);
        return await playGameBLL.DisplayGrade(displayGradeRequest.DisplayGrade, displayGradeRequest.GameId);
    }


    /// <summary>
    /// 获取可重连的会话列表
    /// </summary>
    /// <param name="requestBaseModel"></param>
    /// <returns></returns>
    [HttpPost]
    [Authorize]
    public async Task<PlayGameInfoDto> GetMyScListAsync([FromBody] RequestBaseModel requestBaseModel)
    {
        PlayGameBLL playGameBLL = new PlayGameBLL(ServiceProvider, JYApi);
        return await playGameBLL.GetMyScList(requestBaseModel.Sn);

    }

    /// <summary>
    /// 重连会话
    /// </summary>
    /// <param name="gameRequest"></param>
    /// <returns></returns>
    [HttpPost]
    [Authorize]
    public async Task<BaseResponse<string>> ReconPlayGameAsync([FromBody] GameRequest gameRequest)
    {
        PlayGameBLL playGameBLL = new PlayGameBLL(ServiceProvider, JYApi);
        return await playGameBLL.ReconPlayGame(gameRequest);
    }
}
