package com.ssyf.module.robot.controller.auth;

import cn.hutool.core.bean.BeanUtil;
import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.result.Result;
import com.ssyf.module.robot.domain.entity.RRobotAccount;
import com.ssyf.module.robot.domain.entity.RRobotFee;
import com.ssyf.module.robot.service.RRobotAccountService;
import com.ssyf.module.robot.service.RRobotFeeService;
import com.ssyf.plugs.logger.Log;
import com.ssyf.task.service.RobotManualControlService;
import com.ssyf.task.websocket.RRobotWsMessage;
import com.ssyf.third.bmiss.model.ApiResponse;
import com.ssyf.third.bmiss.model.GiftDetail;
import com.ssyf.third.bmiss.model.LiveRoom;
import com.ssyf.third.bmiss.model.LoginResponse;
import com.ssyf.module.robot.domain.query.RobotManualControlQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机器人手动控制接口
 * 为运营提供手动接管机器人的功能，支持单独操作机器人进行各种互动
 *
 * @author system
 * @since 1.0
 */
@Api(tags="机器人-手动控制")
@RestController
@RequestMapping("/auth/RobotManualControl")
public class RobotManualControlAuthController {

    @Autowired
    private RobotManualControlService robotManualControlService;

    @Autowired
    private RRobotFeeService robotFeeService;

    @Autowired
    private RRobotAccountService robotAccountService;

    @Log(title = "机器人手动控制-获取机器人余额")
    @ApiOperation(value = "获取机器人余额", notes = "获取机器人余额")
    @PostMapping(value = "/getFee")
    public Result<RRobotFee> getFee(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        RRobotAccount robotAccount = robotAccountService.getOneByUid(query.getRobotUuid());
        if(robotAccount == null){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"机器人不存在");
        }

        RRobotFee robotFee = robotFeeService.getOneAvailable(robotAccount);

        return Result.success(robotFee);
    }

    @Log(title = "机器人手动控制-接管机器人")
    @ApiOperation(value = "接管机器人", notes = "接管机器人控制权，默认控制30分钟")
    @PostMapping(value = "/takeOver")
    public Result<Boolean> takeOver(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        boolean success = robotManualControlService.takeOverControl(query.getRobotUuid(), query.getDurationMinutes());
        if (success) {
            return Result.success(true);
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"接管失败，机器人可能已被其他运营接管或不存在");
        }
    }

    @Log(title = "机器人手动控制-释放机器人")
    @ApiOperation(value = "释放机器人", notes = "释放机器人控制权")
    @PostMapping(value = "/release")
    public Result<Boolean> release(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        boolean success = robotManualControlService.releaseControl(query.getRobotUuid());
        if (success) {
            return Result.success(true);
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"释放失败，机器人可能未被接管或不存在");
        }
    }

    @Log(title = "机器人手动控制-机器人登录")
    @ApiOperation(value = "机器人登录", notes = "机器人登录操作")
    @PostMapping(value = "/login")
    public Result<LoginResponse> login(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<LoginResponse> response = robotManualControlService.loginRobot(query.getRobotUuid());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-机器人登出")
    @ApiOperation(value = "机器人登出", notes = "机器人登出操作")
    @PostMapping(value = "/logout")
    public Result<Object> logout(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<Object> response = robotManualControlService.logoutRobot(query.getRobotUuid());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-获取直播间在线列表")
    @ApiOperation(value = "获取直播间在线列表", notes = "获取当前在线的直播间列表")
    @PostMapping(value = "/liveRooms")
    public Result<List<LiveRoom>> getLiveRooms(@Validated @RequestBody RobotManualControlQuery query) {

        ApiResponse<List<LiveRoom>> response = robotManualControlService.getLiveRoomList(query.isRefresh());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-进入直播间")
    @ApiOperation(value = "进入直播间", notes = "机器人进入指定直播间")
    @PostMapping(value = "/enterRoom")
    public Result<Object> enterLiveRoom(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null || query.getRoomId() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<String> response = robotManualControlService.enterLiveRoom(query.getRobotUuid(), query.getRoomId());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-退出直播间")
    @ApiOperation(value = "退出直播间", notes = "机器人退出当前直播间")
    @PostMapping(value = "/quitRoom")
    public Result<Object> quitLiveRoom(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<Object> response = robotManualControlService.quitLiveRoom(query.getRobotUuid());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-点赞直播间")
    @ApiOperation(value = "点赞直播间", notes = "对当前直播间进行点赞")
    @PostMapping(value = "/like")
    public Result<Object> like(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<Object> response = robotManualControlService.likeLiveRoom(query.getRobotUuid());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-关注主播")
    @ApiOperation(value = "关注主播", notes = "对当前直播间主播进行关注")
    @PostMapping(value = "/follow")
    public Result<Object> follow(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<Object> response = robotManualControlService.followAnchor(query.getRobotUuid());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-获取礼物列表")
    @ApiOperation(value = "获取礼物列表", notes = "获取当前直播间的礼物列表")
    @PostMapping(value = "/giftList")
    public Result<List<GiftDetail>> getGiftList(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<List<GiftDetail>> response = robotManualControlService.getGiftList(query.getRobotUuid());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-送礼")
    @ApiOperation(value = "送礼", notes = "对当前直播间进行送礼")
    @PostMapping(value = "/sendGift")
    public Result<Object> sendGift(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null || query.getGiftId() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<Object> response = robotManualControlService.sendGift(query.getRobotUuid(), query.getGiftId());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-发送聊天消息")
    @ApiOperation(value = "发送聊天消息", notes = "在直播间发送聊天消息")
    @PostMapping(value = "/sendMessage")
    public Result<Object> sendMessage(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null || query.getMessage() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<Object> response = robotManualControlService.sendChatMessage(query.getRobotUuid(), query.getMessage());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-获取直播间消息列表")
    @ApiOperation(value = "获取直播间消息列表", notes = "获取当前直播间的消息列表，支持时间范围、分页和增量同步")
    @PostMapping(value = "/getMessages")
    public Result<List<RRobotWsMessage>> getMessages(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<List<RRobotWsMessage>> response = robotManualControlService.getLiveRoomMessages(query.getRobotUuid(), query);
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }

    @Log(title = "机器人手动控制-获取直播间消息总数")
    @ApiOperation(value = "获取直播间消息总数", notes = "获取当前直播间的消息总数")
    @PostMapping(value = "/messageCount")
    public Result<Long> getMessageCount(@Validated @RequestBody RobotManualControlQuery query) {

        if (BeanUtil.isEmpty(query) || query.getRobotUuid() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"参数不能为空");
        }

        ApiResponse<Long> response = robotManualControlService.getLiveRoomMessageCount(query.getRobotUuid());
        if (response.isSuccess()) {
            return Result.success(response.getData());
        } else {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,response.getMessage());
        }
    }
}