package com.yonge.cooleshow.student.controller;

import com.beust.jcommander.internal.Lists;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.TenantGroupAlbumService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantGroupAlbumWrapper;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.student:}/tenantGroupAlbum")
@Api(tags = "机构小组专辑配置表")
public class TenantGroupAlbumController {

    @Autowired
    private TenantGroupAlbumService tenantGroupAlbumService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

	@ApiOperation(value = "学生可购买机构专辑信息")
	@GetMapping("/buyAlbumInfo")
	public HttpResponseResult<List<TenantGroupAlbumWrapper.BuyTenantAlbum>> buyAlbumInfo(@RequestParam(required = false) Long tenantGroupAlbumId
        ,@RequestParam(required = false) Long tenantAlbumId) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return HttpResponseResult.failed(HttpStatus.FORBIDDEN, "请登录");
        }
        TenantGroupAlbumWrapper.BuyTenantAlbumQuery query = new TenantGroupAlbumWrapper.BuyTenantAlbumQuery();
        query.setTenantGroupAlbumId(tenantGroupAlbumId);
        query.setUserId(user.getId());
        query.setClientType(ClientEnum.STUDENT);
        if (tenantAlbumId != null) {
            query.setTenantAlbumIds(Lists.newArrayList(tenantAlbumId));
        }
    
		return HttpResponseResult.succeed(tenantGroupAlbumService.buyAlbumInfo(query));
	}


    @ApiOperation(value = "学生小组专辑信息")
    @GetMapping("/albumList")
    public HttpResponseResult<Collection<TenantGroupAlbumWrapper.BuyTenantAlbum>> albumList() {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return HttpResponseResult.failed(HttpStatus.FORBIDDEN, "请登录");
        }
        TenantGroupAlbumWrapper.BuyTenantAlbumQuery query = new TenantGroupAlbumWrapper.BuyTenantAlbumQuery();
        query.setUserId(user.getId());
        query.setClientType(ClientEnum.STUDENT);
        query.setSortFlag(true);
        List<TenantGroupAlbumWrapper.BuyTenantAlbum> buyTenantAlbums = tenantGroupAlbumService.buyAlbumInfo(query);
        // 同ID去重
        if (CollectionUtils.isEmpty(buyTenantAlbums)) {
            return HttpResponseResult.succeed(new ArrayList<>());
        }
        //buyTenantAlbums 根据 sort 排序，在根据expireTime 排序
        // 根据status和sort两个字段对列表buyTenantAlbums进行排序
        buyTenantAlbums.sort((o1, o2) -> {
            // 首先根据status字段排序
            int statusComparison = Boolean.compare(o2.getStatus(), o1.getStatus());
            if (statusComparison != 0) {
                return statusComparison;
            }

            // 如果status字段相等，则根据sort字段倒序排序
            return Long.compare(o2.getSort(),o1.getSort());
        });

        // 使用一个HashSet去重，根据ID字段对列表去重
        Set<Long> idSet = new HashSet<>();
        List<TenantGroupAlbumWrapper.BuyTenantAlbum> uniqueList = new ArrayList<>();

        // 遍历列表，去重
        for (TenantGroupAlbumWrapper.BuyTenantAlbum obj : buyTenantAlbums) {
            if (idSet.add(obj.getId())) {
                uniqueList.add(obj);
            }
        }


        return HttpResponseResult.succeed(uniqueList);
    }

    @ApiOperation(value = "学生可购买机构专辑信息")
    @GetMapping("/buyAlbumInfoDetail")
    public HttpResponseResult<TenantGroupAlbumWrapper.BuyTenantAlbum> buyAlbumInfoDetail(@RequestParam Long tenantAlbumId) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return HttpResponseResult.failed(HttpStatus.FORBIDDEN, "请登录");
        }

        TenantGroupAlbumWrapper.BuyTenantAlbumQuery query = new TenantGroupAlbumWrapper.BuyTenantAlbumQuery();
        query.setTenantAlbumIds(Lists.newArrayList(tenantAlbumId));
        query.setUserId(user.getId());
        query.setClientType(ClientEnum.STUDENT);
        List<TenantGroupAlbumWrapper.BuyTenantAlbum> boughtAlbumInfo = tenantGroupAlbumService.buyAlbumInfo(query);
        if (CollectionUtils.isNotEmpty(boughtAlbumInfo)) {
            return HttpResponseResult.succeed(boughtAlbumInfo.get(0));
        }
        throw new BizException("未找到专辑信息");
    }
}