package cn.reghao.tnb.file.app.zdisk.service;

import cn.reghao.jutil.jdk.web.db.Page;
import cn.reghao.tnb.auth.api.dto.AccountAvatar;
import cn.reghao.tnb.auth.api.iface.AccountQuery;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.common.util.ConstantId;
import cn.reghao.tnb.common.util.IdService;
import cn.reghao.tnb.common.util.RandomUtil;
import cn.reghao.tnb.file.app.zdisk.db.mapper.CamDeviceMapper;
import cn.reghao.tnb.file.app.zdisk.db.mapper.UserActivityMapper;
import cn.reghao.tnb.file.app.zdisk.model.constant.AlbumType;
import cn.reghao.tnb.file.app.zdisk.model.dto.CamDeviceDto;
import cn.reghao.tnb.file.app.zdisk.model.po.CamDevice;
import cn.reghao.tnb.file.app.zdisk.model.po.UserActivity;
import cn.reghao.tnb.file.app.zdisk.model.query.CamDeviceQuery;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author reghao
 * @date 2024-08-27 19:50:56
 */
@Service
public class CamService {
    @DubboReference(check = false, retries = 0, timeout = 60_000)
    private AccountQuery accountQuery;

    private int pageSize = 10;
    private final IdService idService;
    private final CamDeviceMapper camDeviceMapper;
    private final UserActivityMapper userActivityMapper;
    private final DiskShareService diskShareService;

    public CamService(IdService idService, CamDeviceMapper camDeviceMapper,
                      UserActivityMapper userActivityMapper, DiskShareService diskShareService) {
        this.idService = idService;
        this.camDeviceMapper = camDeviceMapper;
        this.userActivityMapper = userActivityMapper;
        this.diskShareService = diskShareService;
    }

    public void addCamDevice(CamDeviceDto camDeviceDto) {
        String camName = camDeviceDto.getCamName();
        long camId = idService.getLongId();
        CamDevice camDevice = new CamDevice(camId, camName);
        camDeviceMapper.save(camDevice);
    }

    public List<CamDevice> getCamList() {
        long loginUser = UserContext.getUserId();
        CamDeviceQuery camDeviceQuery = new CamDeviceQuery.Builder()
                .addBy(loginUser)
                .build();
        Page page = new Page(1, pageSize);
        List<CamDevice> camDeviceList = camDeviceMapper.findCamDeviceQueryByPage(page, camDeviceQuery);

        List<Long> sharedCamIds = diskShareService.getSharedAlbumIds(AlbumType.cam.getValue(), loginUser);
        if (!sharedCamIds.isEmpty()) {
            List<CamDevice> sharedCamDevices = camDeviceMapper.findByCamIds(sharedCamIds);
            sharedCamDevices.forEach(this::setSharedCamName);
            camDeviceList.addAll(sharedCamDevices);
        }
        return camDeviceList;
    }

    public CamDevice getCamDevice(long camId) {
        Page page = new Page(1, pageSize);
        long loginUser = UserContext.getUserId();
        CamDeviceQuery camDeviceQuery = new CamDeviceQuery.Builder()
                .addBy(loginUser)
                .camId(camId)
                .build();
        List<CamDevice> camDeviceList = camDeviceMapper.findCamDeviceQueryByPage(page, camDeviceQuery);

        if (camDeviceList.isEmpty()) {
            Set<Long> sharedCamIds = new HashSet<>(diskShareService.getSharedAlbumIds(AlbumType.cam.getValue(), loginUser));
            if (sharedCamIds.contains(camId)) {
                camDeviceQuery = new CamDeviceQuery.Builder()
                        .camId(camId)
                        .build();
                camDeviceList = camDeviceMapper.findCamDeviceQueryByPage(page, camDeviceQuery);
                camDeviceList.forEach(this::setSharedCamName);
            }
        }

        return camDeviceList.isEmpty() ? null : camDeviceList.get(0);
    }

    private void setSharedCamName(CamDevice camDevice) {
        long createBy = camDevice.getAddBy();
        AccountAvatar accountAvatar = accountQuery.getAccountAvatar(createBy);
        String username = accountAvatar.getUsername();
        String albumName = String.format("%s(%s的分享)", camDevice.getCamName(), username);
        camDevice.setCamName(albumName);
    }

    public void addActivity() {
        int total = RandomUtil.getNumber(10, 100);
        List<UserActivity> list = new ArrayList<>();
        while (total-- > 0) {
            UserActivity userActivity = new UserActivity();
            userActivity.setCreateBy(ConstantId.ANONYMOUS_USER_ID);
            list.add(userActivity);
        }

        list.add(new UserActivity());
        userActivityMapper.saveAll(list);
    }
}
