package com.sonic.frog.controller.web;

import com.sonic.common.auth.domains.Session;
import com.sonic.common.rpc.Result;
import com.sonic.common.utils.RedisLock;
import com.sonic.frog.domain.input.BatchAddBackgroundInput;
import com.sonic.frog.domain.input.ChatSetInput;
import com.sonic.frog.domain.input.DelBackgroundInput;
import com.sonic.frog.domain.input.SetBackgroundInput;
import com.sonic.frog.domain.output.BackgroundImgListOutput;
import com.sonic.frog.domain.output.BatchAddBackgroundOutput;
import com.sonic.frog.service.ChatUserBackgroundService;
import com.sonic.frog.utils.RedisKeyUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Web-聊天背景
 *
 * @author code
 */
@RestController
@Slf4j
public class ChatBackgroundWeb {

    @Autowired
    private ChatUserBackgroundService chatUserBackgroundService;
    @Autowired
    private RedisLock.RedisWrapper redisWrapper;
    @Autowired
    private RedisKeyUtils redisKeyUtils;

    @ApiOperation(value = "批量添加背景", tags = {"Web-聊天背景"})
    @PostMapping("/web/chat-background/batch-add")
    public Result<List<Long>> batchAddBackground(@Validated @RequestBody BatchAddBackgroundInput input, @ApiParam(hidden = true) Session session) {
        AtomicReference atomicReference = new AtomicReference();
        //操作人加锁，防止并发操作
        String key = redisKeyUtils.backgroundAddLockKey(session.getUserId());
        RedisLock redisLock = new RedisLock(key, redisWrapper);
        redisLock.tryAcquireRun(() -> {
            //上传后返回主键id列表给前端
            List<Long> ids = chatUserBackgroundService.batchAddBackground(session.getUserId(), input);
            //设置返回对象
            atomicReference.set(BatchAddBackgroundOutput.builder().ids(ids).build());
            return true;
        });
        return Result.success(atomicReference.get());
    }

    @ApiOperation(value = "获取聊天背景列表", tags = {"Web-聊天背景"})
    @PostMapping("/web/chat-background/list")
    public Result<BackgroundImgListOutput> getBackgroundImgList(@Validated @RequestBody ChatSetInput input, @ApiParam(hidden = true) Session session) {
        return Result.success(chatUserBackgroundService.getBackgroundImgList(session.getUserId(), input.getAiId()));
    }

    @ApiOperation(value = "设置聊天背景", tags = {"Web-聊天背景"})
    @PostMapping("/web/chat-background/set-background")
    public Result<Void> setBackground(@Validated @RequestBody SetBackgroundInput input, @ApiParam(hidden = true) Session session) {
        chatUserBackgroundService.setBackground(session.getUserId(), input);
        return Result.success();
    }

    @ApiOperation(value = "删除聊天背景", tags = {"Web-聊天背景"})
    @PostMapping("/web/chat-background/del")
    public Result<BackgroundImgListOutput> delBackground(@Validated @RequestBody DelBackgroundInput input, @ApiParam(hidden = true) Session session) {
        chatUserBackgroundService.delBackground(session.getUserId(), input.getBackgroundId());
        return Result.success();
    }

}
