package cn.com.tenlion.materialstore.service.materialdata.impl;

import cn.com.tenlion.materialstore.pojo.MaterialTypeEnum;
import cn.com.tenlion.materialstore.pojo.dtos.materialaudio.MaterialAudioDTO;
import cn.com.tenlion.materialstore.pojo.dtos.materialdata.MaterialDataDTO;
import cn.com.tenlion.materialstore.pojo.dtos.materiallink.MaterialLinkDTO;
import cn.com.tenlion.materialstore.pojo.dtos.materialmap.MaterialMapDTO;
import cn.com.tenlion.materialstore.pojo.dtos.materialphoto.MaterialPhotoDTO;
import cn.com.tenlion.materialstore.pojo.dtos.materialtext.MaterialTextDTO;
import cn.com.tenlion.materialstore.pojo.dtos.materialvideo.MaterialVideoDTO;
import cn.com.tenlion.materialstore.pojo.vos.materialaudio.MaterialAudioVO;
import cn.com.tenlion.materialstore.pojo.vos.materialdata.MaterialDataVO;
import cn.com.tenlion.materialstore.pojo.vos.materiallink.MaterialLinkVO;
import cn.com.tenlion.materialstore.pojo.vos.materialmap.MaterialMapVO;
import cn.com.tenlion.materialstore.pojo.vos.materialphoto.MaterialPhotoVO;
import cn.com.tenlion.materialstore.pojo.vos.materialtext.MaterialTextVO;
import cn.com.tenlion.materialstore.pojo.vos.materialvideo.MaterialVideoVO;
import cn.com.tenlion.materialstore.service.materialaudio.IMaterialAudioService;
import cn.com.tenlion.materialstore.service.materialdata.MaterialDataService;
import cn.com.tenlion.materialstore.service.materiallink.IMaterialLinkService;
import cn.com.tenlion.materialstore.service.materialmap.IMaterialMapService;
import cn.com.tenlion.materialstore.service.materialphoto.IMaterialPhotoService;
import cn.com.tenlion.materialstore.service.materialtext.IMaterialTextService;
import cn.com.tenlion.materialstore.service.materialvideo.IMaterialVideoService;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SearchException;
import ink.wgink.pojo.app.AppTokenUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Primary
@Service
public class MaterialDataServiceImpl extends DefaultBaseService implements MaterialDataService {

    @Autowired
    private IMaterialTextService materialTextService;
    @Autowired
    private IMaterialPhotoService materialPhotoService;
    @Autowired
    private IMaterialAudioService materialAudioService;
    @Autowired
    private IMaterialVideoService materialVideoService;
    @Autowired
    private IMaterialMapService materialMapService;
    @Autowired
    private IMaterialLinkService materialLinkService;

    @Override
    public void save(String token, MaterialDataVO dataVO) {
        List<MaterialTextVO> textVOList = dataVO.getText();
        List<MaterialPhotoVO> photoVOList = dataVO.getPhoto();
        List<MaterialAudioVO> audioVOList = dataVO.getAudio();
        List<MaterialVideoVO> videoVOList = dataVO.getVideo();
        List<MaterialMapVO> mapVOList = dataVO.getMap();
        List<MaterialLinkVO> linkVOList = dataVO.getLink();
        /**
         * 文字存储
         */
        materialTextService.removeByMaterialId(dataVO.getMaterialId());
        if(textVOList != null && textVOList.size() > 0) {
            for (MaterialTextVO text : textVOList) {
                text.setMaterialId(dataVO.getMaterialId());
                String textId = materialTextService.saveReturnId(token, text);
            }
        }
        /**
         * 照片存储
         */
        materialPhotoService.removeByMaterialId(dataVO.getMaterialId());
        if(photoVOList != null && photoVOList.size() > 0) {
            for (MaterialPhotoVO photo : photoVOList) {
                photo.setMaterialId(dataVO.getMaterialId());
                String photoId = materialPhotoService.saveReturnId(token, photo);
            }
        }
        /**
         * 音频存储
         */
        materialAudioService.removeByMaterialId(dataVO.getMaterialId());
        if(audioVOList != null && audioVOList.size() > 0) {
            for (MaterialAudioVO audio : audioVOList) {
                audio.setMaterialId(dataVO.getMaterialId());
                String audioId = materialAudioService.saveReturnId(token, audio);
            }
        }
        /**
         * 视频存储
         */
        materialVideoService.removeByMaterialId(dataVO.getMaterialId());
        if(videoVOList != null && videoVOList.size() > 0) {
            for (MaterialVideoVO video : videoVOList) {
                video.setMaterialId(dataVO.getMaterialId());
                String videoId = materialVideoService.saveReturnId(token, video);
            }
        }
        /**
         * 经纬度存储
         */
        materialMapService.removeByMaterialId(dataVO.getMaterialId());
        if(mapVOList != null && mapVOList.size() > 0) {
            for (MaterialMapVO map : mapVOList) {
                map.setMaterialId(dataVO.getMaterialId());
                String mapId = materialMapService.saveReturnId(token, map);
            }
        }
        /**
         * 链接存储
         */
        materialLinkService.removeByMaterialId(dataVO.getMaterialId());
        if(linkVOList != null && linkVOList.size() > 0) {
            for (MaterialLinkVO link : linkVOList) {
                link.setMaterialId(dataVO.getMaterialId());
                String linkId = materialLinkService.saveReturnId(token, link);
            }
        }
    }

    @Override
    public MaterialDataDTO getByMaterialId(String materialId) {
        MaterialDataDTO dataDTO = new MaterialDataDTO();
        List<MaterialTextDTO> textDTOList = materialTextService.getByMaterialId(materialId);
        List<MaterialPhotoDTO> photoDTOList = materialPhotoService.getByMaterialId(materialId);
        List<MaterialAudioDTO> audioDTOList = materialAudioService.getByMaterialId(materialId);
        List<MaterialVideoDTO> videoDTOList = materialVideoService.getByMaterialId(materialId);
        List<MaterialMapDTO> mapDTOList = materialMapService.getByMaterialId(materialId);
        List<MaterialLinkDTO> linkDTOList = materialLinkService.getByMaterialId(materialId);
        dataDTO.setText(textDTOList);
        dataDTO.setPhoto(photoDTOList);
        dataDTO.setAudio(audioDTOList);
        dataDTO.setVideo(videoDTOList);
        dataDTO.setMap(mapDTOList);
        dataDTO.setLink(linkDTOList);
        return dataDTO;
    }

    @Override
    public MaterialDataDTO getByToken(String token, MaterialTypeEnum typeEnum) throws Exception {
        MaterialDataDTO dataDTO = new MaterialDataDTO();
        Map<String, Object> query = new HashMap<String, Object>();
        AppTokenUser user = securityComponent.getAppTokenUser(token);
        if(user == null || StringUtils.isEmpty(user.getId())) {
            throw new SearchException("用户信息不存在");
        }
        query.put("creator", user.getId());
        switch (typeEnum) {
            case TEXT:
                List<MaterialTextDTO> textDTOList = materialTextService.list(query);
                dataDTO.setText(textDTOList);
                break;
            case PHOTO:
                List<MaterialPhotoDTO> photoDTOList = materialPhotoService.list(query);
                dataDTO.setPhoto(photoDTOList);
                break;
            case AUDIO:
                List<MaterialAudioDTO> audioDTOList = materialAudioService.list(query);
                dataDTO.setAudio(audioDTOList);
                break;
            case VIDEO:
                List<MaterialVideoDTO> videoDTOList = materialVideoService.list(query);
                dataDTO.setVideo(videoDTOList);
                break;
            case MAP:
                List<MaterialMapDTO> mapDTOList = materialMapService.list(query);
                dataDTO.setMap(mapDTOList);
                break;
            case LINK:
                List<MaterialLinkDTO> linkDTOList = materialLinkService.list(query);
                dataDTO.setLink(linkDTOList);
                break;
            default:
                dataDTO.setText(materialTextService.list(query));
                dataDTO.setPhoto(materialPhotoService.list(query));
                dataDTO.setAudio(materialAudioService.list(query));
                dataDTO.setVideo(materialVideoService.list(query));
                dataDTO.setMap(materialMapService.list(query));
                dataDTO.setLink(materialLinkService.list(query));
        }
        return dataDTO;
    }

}
