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.MeetInput;
import com.sonic.frog.domain.input.MeetSdInput;
import com.sonic.frog.domain.input.MeetUnlockInput;
import com.sonic.frog.domain.output.AiAlbumDetailOutput;
import com.sonic.frog.domain.output.AiUserBaseOutput;
import com.sonic.frog.domain.output.MeetSdOutput;
import com.sonic.frog.service.AiUserAlbumService;
import com.sonic.frog.service.AiUserSearchService;
import com.sonic.frog.service.MeetService;
import com.sonic.frog.service.UserAiMeetService;
import com.sonic.frog.utils.RedisKeyUtils;
import com.sonic.sdk.api.annotation.IgnoreAuth;
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.security.InvalidKeyException;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Web-Meet遇见
 *
 * @author code
 */
@RestController
@Slf4j
public class AiUserMeetWeb {

    @Autowired
    private UserAiMeetService userAiMeetService;
    @Autowired
    private AiUserSearchService aiUserSearchService;
    @Autowired
    private AiUserAlbumService aiUserAlbumService;
    @Autowired
    private MeetService meetService;
    @Autowired
    private RedisLock.RedisWrapper redisWrapper;
    @Autowired
    private RedisKeyUtils redisKeyUtils;

    @IgnoreAuth
    @ApiOperation(value = "上报滑动次数", tags = {"Web-Meet遇见"})
    @PostMapping("/web/meet/sd")
    public Result<MeetSdOutput> meetSd(@Validated @RequestBody MeetSdInput input, @ApiParam(hidden = true) Session session) {
        AtomicReference<MeetSdOutput> meetSdOutput = new AtomicReference();
        //加锁处理
        RedisLock redisLock = new RedisLock(redisKeyUtils.meetSdLockKey(session.getUserId()), redisWrapper);
        redisLock.tryAcquireRun(() -> {
            meetSdOutput.set(userAiMeetService.meetSd(session.getUserId(), input.getAiId(), input.getLk()));;
            return true;
        });
        return Result.success(meetSdOutput.get());
    }

    @IgnoreAuth
    @ApiOperation(value = "上报meet绑定", tags = {"Web-Meet遇见"})
    @PostMapping("/web/meet/bd")
    public Result<AiUserBaseOutput> meetBd(@Validated @RequestBody MeetInput input, @ApiParam(hidden = true) Session session) {
        boolean bl = userAiMeetService.meetBd(session.getUserId(), input.getAiId());
        if(bl) {
            return Result.success(aiUserSearchService.getAiUserBaseInfo(session.getUserId(), input.getAiId()));
        }
        return Result.success();
    }

    @IgnoreAuth
    @ApiOperation(value = "上报meet爱慕者推荐并返回模糊相册ID", tags = {"Web-Meet遇见"})
    @PostMapping("/web/meet/rc")
    public Result<AiAlbumDetailOutput> meetRc(@ApiParam(hidden = true) Session session) throws InvalidKeyException {
        //随机推荐AI
        Long albumId = userAiMeetService.meetRc(session.getUserId());
        if(albumId == null) {
            return Result.success();
        }
        return Result.success(aiUserAlbumService.rcDetail(albumId));
    }

    @IgnoreAuth
    @ApiOperation(value = "meet爱慕者解锁", tags = {"Web-Meet遇见"})
    @PostMapping("/web/meet/unlock")
    public Result<Void> meetUnLock(@RequestBody MeetUnlockInput input, @ApiParam(hidden = true) Session session) {
        //购买，产生流水
        meetService.meetUnLock(session.getUserId(), input);
        return Result.success();
    }

}
