/*
 * Copyright 2018 - 2050 zyp.All Rights Reserved.
 *
 */
package com.sonic.frog.controller.web;

import com.sonic.common.AppRuntime;
import com.sonic.common.auth.domains.Session;
import com.sonic.common.rpc.Page;
import com.sonic.common.rpc.Result;
import com.sonic.common.utils.IpAddressUtils;
import com.sonic.common.utils.RedisLock;
import com.sonic.frog.domain.entity.Liked;
import com.sonic.frog.domain.input.*;
import com.sonic.frog.domain.output.BatchAddAlbumOutput;
import com.sonic.frog.domain.output.ListAiAlbumOutput;
import com.sonic.frog.domain.output.ViewUnlockAlbumImgOutput;
import com.sonic.frog.limit.RequestLimit;
import com.sonic.frog.service.AiUserAlbumService;
import com.sonic.frog.service.UserCreateCountStatService;
import com.sonic.frog.utils.RedisKeyUtils;
import com.sonic.sdk.api.annotation.IgnoreAuth;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Web-Ai用户相册
 *
 * @author mzc
 * @createTime 2019-06-26 19:47:58
 */

@RestController
public class AiUserAlbumWeb {

    @Autowired
    private AppRuntime appRuntime;
    @Autowired
    private RedisLock.RedisWrapper redisWrapper;
    @Autowired
    private AiUserAlbumService aiUserAlbumService;
    @Autowired
    private UserCreateCountStatService userCreateCountStatService;
    @Autowired
    private RedisKeyUtils redisKeyUtils;


    @RequestLimit(count = 1, loginCount = 10, message = "operate too frequently，please try again later (101) ")
    @ApiOperation(value = "批量添加图片到相册", tags = {"WEB-Ai用户相册"})
    @PostMapping(value = "/web/ai-user/batch-add-album")
    public Result<BatchAddAlbumOutput> batchAddAlbum(@Validated @RequestBody BatchAddAlbumInput addAlbum, @ApiParam(hidden = true) Session session, HttpServletRequest request) {
        AtomicReference atomicReference = new AtomicReference();
        //操作人加锁，防止并发操作
        RedisLock redisLock = new RedisLock(appRuntime.buildPrefixKey(Liked.BizType.ALBUM_PIC.name(), "add", session.getUserId()), redisWrapper);
        redisLock.tryAcquireRun(() -> {
            //上传后返回主键id列表给前端
            List<Long> ids = aiUserAlbumService.batchAddAlbum(session.getUserId(), addAlbum);
            //设置返回对象
            atomicReference.set(BatchAddAlbumOutput.builder().ids(ids).build());
            return true;
        });
        return Result.success(atomicReference.get());
    }

    @RequestLimit(count = 30, loginCount = 40, message = "operate too frequently，please try again later (101) ")
    @ApiOperation(value = "相册列表", tags = {"WEB-Ai用户相册"})
    @PostMapping(value = "/web/ai-user/album-list")
    @IgnoreAuth
    public Result<Page<ListAiAlbumOutput>> listAlbums(@Valid @RequestBody AlbumListInput input, @ApiParam(hidden = true) Session session, HttpServletRequest request) {
        input.setIpAddress(IpAddressUtils.getIpAddress(request));
        Page<ListAiAlbumOutput> listAlbums = aiUserAlbumService.listAlbums(input, session.getUserId());
        return Result.success(listAlbums);
    }

    @ApiOperation(value = "删除图片", tags = {"WEB-Ai用户相册"})
    @PostMapping(value = "/web/ai-user/album-del")
    public Result<Void> delAlbum(@Validated @RequestBody DelAlbumInput input, Session session) {
        aiUserAlbumService.delAlbum(session.getUserId(), input.getAlbumId());
        return Result.success();
    }

    @RequestLimit(count = 30, loginCount = 40, message = "operate too frequently，please try again later (101) ")
    @ApiOperation(value = "相册图片点赞、取消点赞", tags = {"WEB-Ai用户相册"})
    @PostMapping(value = "/web/album/like_or_cancel")
    public Result<Void> likeOrCancel(@Validated @RequestBody LikeOrCancelPicInput input, @ApiParam(hidden = true) Session session, HttpServletRequest request) {
        //操作人加锁，防止并发操作
        RedisLock redisLock = new RedisLock(appRuntime.buildPrefixKey(Liked.BizType.ALBUM_PIC.name(), "liked", session.getUserId(), input.getAlbumId()), redisWrapper);
        redisLock.tryAcquireRun(() -> {
            aiUserAlbumService.likeOrCancelPic(session.getUserId(), input);
            return true;
        });
        return Result.success();
    }

    @RequestLimit(count = 30, loginCount = 40, message = "operate too frequently，please try again later (101) ")
    @ApiOperation(value = "设置默认图片", tags = {"WEB-Ai用户相册"})
    @PostMapping(value = "/web/ai-user/set-default-album")
    public Result<Void> setDefaultAlbum(@Validated @RequestBody SetDefaultAlbumInput input, @ApiParam(hidden = true) Session session, HttpServletRequest request) {
        aiUserAlbumService.setDefaultAlbum(session.getUserId(), input.getAiId(), input.getAlbumId());
        return Result.success();
    }

    @RequestLimit(count = 30, loginCount = 40, message = "operate too frequently，please try again later (101) ")
    @ApiOperation(value = "设置相册解锁价格", tags = {"WEB-Ai用户相册"})
    @PostMapping(value = "/web/ai-user/set-album-unlock-price")
    public Result<Void> setAlbumUnlockPrice(@Validated @RequestBody SetAlbumUnlockPriceInput input, @ApiParam(hidden = true) Session session, HttpServletRequest request) {
        aiUserAlbumService.setAlbumUnlockPrice(session.getUserId(), input);
        return Result.success();
    }

    @ApiOperation(value = "解锁加密图片", tags = {"WEB-Ai用户相册"})
    @PostMapping(value = "/web/ai-user/unlock-album-img")
    public Result<ViewUnlockAlbumImgOutput> unlockAlbumImg(@Validated @RequestBody UnlockAlbumImgInput input, @ApiParam(hidden = true) Session session, HttpServletRequest request) {
        //操作人加锁，防止并发操作
        String redisKey = redisKeyUtils.unlockAlbumImgLockKey(session.getUserId(), input.getAlbumId());
        RedisLock redisLock = new RedisLock(redisKey, redisWrapper);
        ViewUnlockAlbumImgOutput output = redisLock.tryAcquireRun(() -> aiUserAlbumService.unlockAlbumImg(session.getUserId(), input));
        return Result.success(output);
    }

    @ApiOperation(value = "加密图片解锁后访问", tags = {"WEB-Ai用户相册"})
    @PostMapping(value = "/web/ai-user/view-unlock-album-img")
    public Result<ViewUnlockAlbumImgOutput> viewUnlockAlbumImg(@Validated @RequestBody ViewUnlockAlbumImgInput input, @ApiParam(hidden = true) Session session, HttpServletRequest request) {
        input.setIpAddress(IpAddressUtils.getIpAddress(request));
        ViewUnlockAlbumImgOutput output = aiUserAlbumService.viewUnlockAlbumImg(session.getUserId(), input);
        return Result.success(output);
    }

    @ApiOperation(value = "获取用户相册创作次数", tags = {"Web-用户管理"})
    @PostMapping("/web/ai-user/get-user-create-count")
    public Result<Boolean> getUserCreateCount(@ApiParam(hidden = true) Session session) {
        return Result.success(userCreateCountStatService.getUserCreateCount(session.getUserId()));
    }
}
