package com.ai.da.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.utils.DateUtil;
import com.ai.da.common.utils.MinioUtil;
import com.ai.da.mapper.primary.*;
import com.ai.da.mapper.primary.entity.TDesignPythonOutfit;
import com.ai.da.mapper.primary.entity.UserLike;
import com.ai.da.mapper.primary.entity.UserLikeGroup;
import com.ai.da.model.vo.*;
import com.ai.da.service.AccountService;
import com.ai.da.service.CollectionService;
import com.ai.da.service.UserLikeGroupService;
import com.ai.da.service.UserLikeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.TimeZone;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author yanglei
 * @since 2022-09-30
 */
@Slf4j
@Service
public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, UserLikeGroup> implements UserLikeGroupService {
    @Resource
    private UserLikeGroupMapper userLikeGroupMapper;
    @Resource
    private AccountService accountService;
    @Resource
    private CollectionService collectionService;
    @Resource
    private UserLikeService userLikeService;

    @Resource
    private UserLikeMapper userLikeMapper;

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private TDesignPythonOutfitMapper designPythonOutfitMapper;
    @Resource
    private DesignMapper designMapper;
    @Resource
    private SysFileMapper sysFileMapper;
    @Resource
    private LibraryMapper libraryMapper;

    @Override
    public void deleteUserGroup(Long userGroupId) {
        UserLikeGroup group = getById(userGroupId);
        if (Objects.isNull(group)) {
            throw new BusinessException("history.not.found");
        }
        userLikeGroupMapper.deleteById(userGroupId);
    }

    @Override
    public HistoryUpdateVO updateUserGroupName(Long userGroupId, String userGroupName, String timeZone) {
        UserLikeGroup group = getById(userGroupId);
        if (Objects.isNull(group)) {
            throw new BusinessException("history.not.found");
        }
        QueryWrapper<UserLikeGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userGroupId);
        UserLikeGroup userLikeGroup = new UserLikeGroup();
        userLikeGroup.setName(userGroupName);
        userLikeGroup.setUpdateDate(DateUtil.getByTimeZone(timeZone));
        log.info("跟新group名字###{}###时间是###{}", userLikeGroup.getUpdateDate(), TimeZone.getDefault());
        userLikeGroupMapper.update(userLikeGroup, queryWrapper);
        return new HistoryUpdateVO(userLikeGroup.getName(), userLikeGroup.getUpdateDate().getTime());
    }

//    public static void main(String[] args) {
//        System.out.println(TimeZone.getDefault());
//        Date kolkata =DateUtil.getByTimeZone("Asia/Kolkata");
//        Date shanghai =DateUtil.getByTimeZone("Asia/Shanghai");
//        System.out.println(kolkata);
//        System.out.println(shanghai);
//        System.out.println(kolkata.getTime());
//        System.out.println(shanghai.getTime());
//        System.out.println(kolkata.getTime() == shanghai.getTime());
//    }

    private List<UserLikeGroup> selectByUserId(Long userId) {
        QueryWrapper<UserLikeGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accountId", userId);
        return userLikeGroupMapper.selectList(queryWrapper);
    }

    @Override
    public Long insertUserGroup(Long userId, Long collectionId, String timeZone) {
        UserLikeGroup userLikeGroup = new UserLikeGroup();
        //默认当前时间
        userLikeGroup.setName(DateUtil.dateToStr(new Date(), DateUtil.YYYY_MM_DD_HH_MM_SS));
        userLikeGroup.setCreateDate(DateUtil.getByTimeZone(timeZone));
        userLikeGroup.setUpdateDate(DateUtil.getByTimeZone(timeZone));
        userLikeGroup.setAccountId(userId);
        userLikeGroup.setCollectionId(collectionId);
        userLikeGroupMapper.insertUserLikeGroup(userLikeGroup);
        return userLikeGroup.getId();
    }

    @Override
    public UserLikeChooseVO choose(Long userGroupId) {
        UserLikeGroup group = getById(userGroupId);
        if (Objects.isNull(group)) {
            throw new BusinessException("history.not.found");
        }
        List<UserLikeVO> userLikeVOS = userLikeService.getGroupDetail(userGroupId);
        String sex = null;
//        if (CollectionUtil.isNotEmpty(userLikeVOS)) {
//            Long designId = userLikeVOS.get(0).getDesignId();
//            Design design = designMapper.selectById(designId);
//            if (design.getModelType().equals(ModelType.SYSTEM.getValue())) {
//                sex = sysFileMapper.selectById(design.getTemplateId()).getLevel2Type();
//            }else {
//                sex = libraryMapper.selectById(design.getTemplateId()).getLevel2Type();
//            }
//        }
        userLikeVOS.forEach(o -> {
            TDesignPythonOutfit tDesignPythonOutfit1 = designPythonOutfitMapper.selectById(o.getDesignOutfitId());
            o.setUrl(tDesignPythonOutfit1.getDesignUrl());
            if (o.getUrl().contains("/")) {
                int index = o.getUrl().lastIndexOf("/");
                o.setPictureName(o.getUrl().substring(index + 1));
            }
            o.setDesignOutfitUrl(minioUtil.getPresignedUrl(o.getUrl(), 24 * 60));
            QueryWrapper<TDesignPythonOutfit> qw = new QueryWrapper<>();
            qw.lambda().eq(TDesignPythonOutfit::getDesignItemId, o.getDesignItemId());
            List<TDesignPythonOutfit> tDesignPythonOutfits = designPythonOutfitMapper.selectList(qw);
            if (CollectionUtil.isNotEmpty(tDesignPythonOutfits)) {
                TDesignPythonOutfit tDesignPythonOutfit = tDesignPythonOutfits.get(0);
                o.setDesignOutfitId(tDesignPythonOutfit.getId());
            }
        });
        UserLikeCollectionVO userLikeCollection = collectionService.chooseCollection(group.getCollectionId());
        return new UserLikeChooseVO(userGroupId, userLikeVOS, userLikeCollection, sex);
    }

    @Override
    public void deleteTrialData(Long userId) {
        QueryWrapper<UserLikeGroup> qw = new QueryWrapper<>();
        qw.lambda().eq(UserLikeGroup::getAccountId, userId);
        List<Long> userLikeGroupIdList = userLikeGroupMapper.selectList(qw).stream()
                .map(UserLikeGroup::getId)
                .collect(Collectors.toList());
        userLikeGroupMapper.delete(qw);
        if (CollectionUtil.isNotEmpty(userLikeGroupIdList)) {
            QueryWrapper<UserLike> userLikeQueryWrapper = new QueryWrapper<>();
            userLikeQueryWrapper.lambda().in(UserLike::getUserLikeGroupId, userLikeGroupIdList);
            userLikeMapper.delete(userLikeQueryWrapper);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDate(Long id,String timeZone){
        UpdateWrapper<UserLikeGroup> uw = new UpdateWrapper<>();
        uw.eq("id",id);
        uw.set("update_date",DateUtil.getByTimeZone(timeZone));

        baseMapper.update(null,uw);
    }
}
