package com.YuyeTech.TPlat.game.imageInfo.service;

import com.YuyeTech.TPlat.common.pojo.Result;
import com.YuyeTech.TPlat.common.service.CommonServiceImpl;
import com.YuyeTech.TPlat.enums.PictureType;
import com.YuyeTech.TPlat.game.gameInfo.projo.GamePicture;
import com.YuyeTech.TPlat.game.gameInfo.service.GameInfoService;
import com.YuyeTech.TPlat.game.gameInfo.service.GamePictureService;
import com.YuyeTech.TPlat.game.gameInfo.vo.GameInfoVo;
import com.YuyeTech.TPlat.game.imageInfo.projo.Pictures;
import com.YuyeTech.TPlat.game.imageInfo.repository.PicturesRepository;
import com.YuyeTech.TPlat.game.imageInfo.service.PicturesService;
import com.YuyeTech.TPlat.game.imageInfo.vo.PicturesVo;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
import com.YuyeTech.TPlat.utils.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:slambb
 * @date:2020/6/24
 */
@Slf4j
@Service
@Transactional
public class PicturesServiceImpl extends CommonServiceImpl<PicturesVo, Pictures, Integer> implements PicturesService {
    @Autowired
    private PicturesRepository picturesRepository;
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private GameInfoService gameInfoService;
    @Autowired
    private GamePictureService gamePictureService;


    @Override
    public Page<Pictures> findAllByPageable(Pageable pageable) {
        return picturesRepository.findAll(pageable);
    }

    @Override
    public Pictures addPicture(Pictures pictures) {
        return picturesRepository.save(pictures);
    }

    @Override
    public Pictures addPictureAndFlush(Pictures pictures) {
        return picturesRepository.saveAndFlush(pictures);
    }


    @Override
    public Pictures findById(Integer id) {
        return picturesRepository.findById(id).orElse(null);
    }

    @Override
    public void deletePictureById(Integer id) {

        PicturesVo picturesVo = super.getVo(id);
        if(picturesVo !=null){
            picturesRepository.deleteById(id);
        }
        if (aliyunOSSUtil.deleteBlog(picturesVo.getPictureUrl())) {
            //删除图片成功
        }
    }

    @Override
    public List<Pictures> findAllByPictureIdList(List<Integer> ids) {

        List<Pictures> picturesList = picturesRepository.findByPictureIdIn(ids).stream().map(e -> {
            e.setPictureUrl(aliyunOSSUtil.addDomainName(e.getPictureUrl()));
            return e;
        }).collect(Collectors.toList());

        return picturesList;
    }

    @Override
    public List<String> findAllByPictureIdListAndNoDomain(List<Integer> ids) {
        List<String> picturesList = picturesRepository.findByPictureIdIn(ids).stream().map(Pictures::getPictureUrl).collect(Collectors.toList());
        return picturesList;
    }

    @Override
    public List<String> findAllByPictureIdListAndDomain(List<Integer> ids) {
        List<String> picturesList = picturesRepository.findByPictureIdIn(ids).stream().map(
                e -> aliyunOSSUtil.addDomainName(e.getPictureUrl())
        ).collect(Collectors.toList());
        return picturesList;
    }

    /**
     * 保存游戏相关图片
     * 1.查询图片库对应，信息查询是否存在关联数据
     * 3.图片库存在对应图片，替换信息，此时处理删除oss上存储的图片
     * 4.存储图片文件到oss，并且保存新的图片url到数据库
     *
     * @param uploadIconFile
     * @param type
     * @param gameInfoVo
     */
    @Override
    public void addPictureToTheGame(File uploadIconFile, String type, GameInfoVo gameInfoVo) {
        String uploadUrl = "";
        //写入图片库
        PicturesVo pictureIcon = new PicturesVo();
        if (type.equals("icon")) {
            List<Pictures> iconList = picturesRepository.findByPictureTypeAndAssociatedId(PictureType.ICON.ordinal(), gameInfoVo.getGameId());
            if (iconList.size() > 1) {
                throw new RuntimeException("icon 图片数量异常！");
            }
            //如果图片库存在游戏的icon
            if (iconList.size() != 0) {
                //删除图片库信息，删除oss信息
                Pictures picture = iconList.get(0);
                if (aliyunOSSUtil.deleteBlog(picture.getPictureUrl())) {
                    //替换图片信息
                    pictureIcon = CopyUtil.copy(picture, PicturesVo.class);
                }
            } else {
                pictureIcon.setPictureType(PictureType.ICON.ordinal());
            }
            //游戏图标，上传到OSS
            uploadUrl = aliyunOSSUtil.upload(uploadIconFile, "gameIcon");
            gameInfoVo.setGameIcon(uploadUrl);
            //更新游戏数据
            gameInfoService.save(gameInfoVo);
        } else if (type.equals("picture")) {
            List<Pictures> coverList = picturesRepository.findByPictureTypeAndAssociatedId(PictureType.COVER.ordinal(), gameInfoVo.getGameId());
            if (coverList.size() > 1) {
                throw new RuntimeException("cover 图片异常！");
            }
            //如果图片库存在游戏的icon
            if (coverList.size() != 0) {
                //删除图片库信息，删除oss信息
                Pictures picture = coverList.get(0);
                if (aliyunOSSUtil.deleteBlog(picture.getPictureUrl())) {
                    //替换图片信息
                    pictureIcon = CopyUtil.copy(picture, PicturesVo.class);
                }
            } else {
                pictureIcon.setPictureType(PictureType.COVER.ordinal());
            }
            //游戏封面，上传到OSS
            uploadUrl = aliyunOSSUtil.upload(uploadIconFile, "pictures");
            gameInfoVo.setGamePicture(uploadUrl);
            //更新游戏数据
            gameInfoService.save(gameInfoVo);
        } else if (type.equals("scroll")) {
            //保存数据时候，不做单独删除操作，仅新添加图片
            //上传到OSS
            uploadUrl = aliyunOSSUtil.upload(uploadIconFile, "pictures");
            //向Pictures
            pictureIcon.setPictureType(PictureType.GAME_SCROLL.ordinal());
        }
        //写入图库
        pictureIcon.setPictureUrl(uploadUrl);
        pictureIcon.setAssociatedId(gameInfoVo.getGameId());//记录一个gameId
        super.saveReturnVo(pictureIcon);
    }

    @Override
    public PicturesVo findByGameIdAndPictureType() {
        return null;
    }


    @Override
    public List<Pictures> findByPictureTypeAndAssociatedId(Integer pictureType, String associatedId) {
        return picturesRepository.findByPictureTypeAndAssociatedId(pictureType, associatedId);
    }


    @Override
    public List<Pictures> findByPictureIdIn(List<Integer> pictureIds) {
        return null;
    }
}
