package com.platform.modules.qk.service.impl;

import com.aliyuncs.AcsRequest;
import com.aliyuncs.AcsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.autoconfigure.live.LiveProperties;
import com.platform.common.utils.ConverterUtils;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.Query;
import com.platform.modules.live.constants.SceneConstants;
import com.platform.modules.live.dao.LiveMultimediaDao;
import com.platform.modules.live.entity.LiveMultimediaEntity;
import com.platform.modules.live.utils.ForeignProperties;
import com.platform.modules.plm.util.FileCalculateUtils;
import com.platform.modules.plm.util.FileSizeUnitEnum;
import com.platform.modules.plm.util.MaterialUtils;
import com.platform.modules.qk.dao.EditProjectDao;
import com.platform.modules.qk.dao.EditProjectMaterialDao;
import com.platform.modules.qk.dao.EditProjectWatermarkDao;
import com.platform.modules.qk.entity.EditProjectEntity;
import com.platform.modules.qk.entity.EditProjectMaterialEntity;
import com.platform.modules.qk.entity.EditProjectWatermarkEntity;
import com.platform.modules.qk.service.VideoService;
import com.platform.plugs.live.LiveClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional
public class VideoServiceImpl extends ServiceImpl<EditProjectMaterialDao, EditProjectMaterialEntity> implements VideoService {

    private DefaultAcsClient client;

    public VideoServiceImpl(LiveProperties liveProperties) {
        String accessKeyId = liveProperties.getAccount().getAccessKeyId();
        String accessKeySecret = liveProperties.getAccount().getSecret();
        this.client = new DefaultAcsClient(DefaultProfile.getProfile(liveProperties.getCommon().getRegionId(),
                accessKeyId, accessKeySecret));
    }

    @Autowired
    private EditProjectMaterialDao editProjectMaterialDao;
    @Autowired
    private EditProjectWatermarkDao editProjectWatermarkDao;
    @Autowired
    private EditProjectDao editProjectDao;
    @Autowired
    private ForeignProperties foreignProperties;
    @Autowired
    private LiveMultimediaDao liveMultimediaDao;
    @Autowired
    private LiveClient liveClient;

    @Override
    public void insertProject(EditProjectEntity evo) {
        evo.setCreateDate(new Date());
        editProjectDao.insert(evo);
    }

    public void insertOrUpdateProject(EditProjectEntity evo) {
        if (evo.getProjectId() != null) {
            Map map = new HashMap();
            map.put("project_id", evo.getProjectId());
            List list = editProjectDao.selectByMap(map);
            if (list != null && list.size() > 0) {
                //更新
                updateProjectByProjectId(evo);
            } else {
                //插入
                insertProject(evo);
            }
        }
    }

    @Override
    public void updateProjectByProjectId(EditProjectEntity evo) {
        evo.setModifiedTime(new Date());
        EntityWrapper<EditProjectEntity> wrapper = new EntityWrapper<EditProjectEntity>();
        wrapper.eq("project_id", evo.getProjectId());
        editProjectDao.update(evo, wrapper);
    }

    @Override
    public void deleteProjectByProjectIds(List<String> listId) {
        EntityWrapper<EditProjectEntity> wrapper = new EntityWrapper<EditProjectEntity>();
        wrapper.in("project_id", listId);
        editProjectDao.delete(wrapper);
    }

    @Override
    public EditProjectEntity findProjectByProjectId(String projectId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("project_id", projectId);
        List<EditProjectEntity> list = queryProjectByMap(map);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<EditProjectEntity> queryProjectByMap(Map<String, Object> map) {
        List<EditProjectEntity> list = editProjectDao.selectByMap(map);
        return list;
    }

    @Override
    public Page<Map> queryProjectPageByMap(Map<String, Object> vmap) {
        int current = ConverterUtils.parseInt(vmap.get("page"));
        int limit = ConverterUtils.parseInt(vmap.get("limit"));
        Page<Map> page = new Page<>(current, limit);
        List<Map> list = editProjectDao.selectList(page, vmap);
        List<HashMap> relist = new ArrayList<HashMap>();
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            Object cover_url = map.get("cover_url");
            String coverUrl = "";
            if (cover_url != null) {
                coverUrl = (String) cover_url;
            }
            map.put("cover_url", MaterialUtils.getMaterialUrl(coverUrl, foreignProperties.getMediaurl()));
            Object media_url = map.get("media_url");
            String mediaUrl = "";
            if (media_url != null) {
                mediaUrl = (String) media_url;
            }
            map.put("media_url", MaterialUtils.getMaterialUrl(mediaUrl, foreignProperties.getMediaurl()));


            Object mediaSize = map.get("media_size");
            if (mediaSize != null) {
                try {
                    Long msize = null;
                    String lsize = "0";

                    if (mediaSize instanceof String) {
                        lsize = (String) mediaSize;
                    } else {
                        msize = mapValueToLong(mediaSize);
                        lsize = (msize < (1024 * 1024) ? FileCalculateUtils.calculateFileSizeString(msize.longValue(), FileSizeUnitEnum.KB) : FileCalculateUtils.calculateFileSizeString(msize.longValue(), FileSizeUnitEnum.MB));
                    }
                    map.put("media_size", lsize);
                } catch (Exception e) {
                    System.out.println(" media_size = " + map.get("media_size"));
                    e.printStackTrace();
                }
            }
            Object mediaDuration = map.get("media_duration");
            if (mediaDuration != null) {
                try {
                    Long md = null;
                    String ld = "0";
                    if (mediaDuration instanceof String) {
                        ld = (String) mediaDuration;
                    } else {
                        md = mapValueToLong(mediaDuration);
                        ld = md + "";
                    }
                    map.put("media_duration", ld);
                } catch (Exception e) {
                    System.out.println(" media_duration = " + map.get("media_duration"));
                    e.printStackTrace();
                }
            }


            try {
                List<String> snapshots = new ArrayList<String>();
                Object obj = map.get("media_snapshots");

                if (obj != null) {
                    String mediaSnapshots = (String) obj;

                    String[] mediaSnapshotsArr = mediaSnapshots.split(",");
                    if (mediaSnapshotsArr != null && mediaSnapshotsArr.length > 0) {
                        for (String str : mediaSnapshotsArr) {
                            if (StringUtils.isNotBlank(str)) {
                                snapshots.add(MaterialUtils.getMaterialUrl(str, foreignProperties.getMediaurl()));
                            }
                        }
                    }
                }
                map.put("media_snapshots", StringUtils.join(snapshots, ","));
            } catch (Exception e) {
                System.out.println(" media_snapshots = " + map.get("media_snapshots"));
                e.printStackTrace();
            }
            relist.add((HashMap) map);
        }

        page.setRecords(list);
        return page;

    }

    @Override
    public List<HashMap> queryProjectListByMap(Map<String, Object> vmap) {
        int current = ConverterUtils.parseInt(vmap.get("page"));
        int limit = ConverterUtils.parseInt(vmap.get("limit"));
        Page<Map> page = new Page<>(current, limit);

        List<Map> list = editProjectDao.selectList(page, vmap);
        List<HashMap> relist = new ArrayList<HashMap>();
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            Object cover_url = map.get("cover_url");
            String coverUrl = "";
            if (cover_url != null) {
                coverUrl = (String) cover_url;
            }
            map.put("cover_url", MaterialUtils.getMaterialUrl(coverUrl, foreignProperties.getMediaurl()));
            Object media_url = map.get("media_url");
            String mediaUrl = "";
            if (media_url != null) {
                mediaUrl = (String) media_url;
            }
            map.put("media_url", MaterialUtils.getMaterialUrl(mediaUrl, foreignProperties.getMediaurl()));


            Object mediaSize = map.get("media_size");
            if (mediaSize != null) {
                try {
                    Long msize = null;
                    String lsize = "0";

                    if (mediaSize instanceof String) {
                        lsize = (String) mediaSize;
                    } else {
                        msize = mapValueToLong(mediaSize);
                        lsize = (msize < (1024 * 1024) ? FileCalculateUtils.calculateFileSizeString(msize.longValue(), FileSizeUnitEnum.KB) : FileCalculateUtils.calculateFileSizeString(msize.longValue(), FileSizeUnitEnum.MB));
                    }
                    map.put("media_size", lsize);
                } catch (Exception e) {
                    System.out.println(" media_size = " + map.get("media_size"));
                    e.printStackTrace();
                }
            }
            Object mediaDuration = map.get("media_duration");
            if (mediaDuration != null) {
                try {
                    Long md = null;
                    String ld = "0";
                    if (mediaDuration instanceof String) {
                        ld = (String) mediaDuration;
                    } else {
                        md = mapValueToLong(mediaDuration);
                        ld = md + "";
                    }
                    map.put("media_duration", ld);
                } catch (Exception e) {
                    System.out.println(" media_duration = " + map.get("media_duration"));
                    e.printStackTrace();
                }
            }


            try {
                List<String> snapshots = new ArrayList<String>();
                Object obj = map.get("media_snapshots");

                if (obj != null) {
                    String mediaSnapshots = (String) obj;

                    String[] mediaSnapshotsArr = mediaSnapshots.split(",");
                    if (mediaSnapshotsArr != null && mediaSnapshotsArr.length > 0) {
                        for (String str : mediaSnapshotsArr) {
                            if (StringUtils.isNotBlank(str)) {
                                snapshots.add(MaterialUtils.getMaterialUrl(str, foreignProperties.getMediaurl()));
                            }
                        }
                    }
                }
                map.put("media_snapshots", StringUtils.join(snapshots, ","));
            } catch (Exception e) {
                System.out.println(" media_snapshots = " + map.get("media_snapshots"));
                e.printStackTrace();
            }
            relist.add((HashMap) map);
        }
        return relist;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String projectId = (String) params.get("projectId");
        String materialType = (String) params.get("materialType");
        Page<EditProjectMaterialEntity> page = this.selectPage(
                new Query<EditProjectMaterialEntity>(params).getPage(),
                new EntityWrapper<EditProjectMaterialEntity>()
                        .eq(StringUtils.isNotBlank(projectId), "project_id", projectId)
                        .eq(StringUtils.isNotBlank(materialType), "material_type", materialType)
                        .eq("is_delete", 0));
        return new PageUtils(page);
    }

    @Override
    public List<HashMap> queryList(Map<String, Object> params) {
        String projectId = (String) params.get("project_id");
        String materialType = (String) params.get("material_type");
        int current = ConverterUtils.parseInt(params.get("page"));
        int limit = ConverterUtils.parseInt(params.get("limit"));
        Page<Map<String, Object>> page = new Page<>(current, limit);

        List<HashMap> list = editProjectMaterialDao.selectList(page, params);
        List<HashMap> relist = new ArrayList<HashMap>();
        for (int i = 0; i < list.size(); i++) {
            HashMap map = list.get(i);
            Object cover_url = map.get("cover_url");
            String coverUrl = "";
            if (cover_url != null) {
                coverUrl = (String) cover_url;
            }
            map.put("cover_url", MaterialUtils.getMaterialUrl(coverUrl, foreignProperties.getMediaurl()));
            Object media_url = map.get("media_url");
            String mediaUrl = "";
            if (media_url != null) {
                mediaUrl = (String) media_url;
            }
            map.put("media_url", MaterialUtils.getMaterialUrl(mediaUrl, foreignProperties.getMediaurl()));

            Object mediaSize = map.get("media_size");
            if (mediaSize != null) {
                try {
                    Long msize = null;
                    String lsize = "0";

                    if (mediaSize instanceof String) {
                        lsize = (String) mediaSize;
                    } else {
                        msize = mapValueToLong(mediaSize);
                        lsize = (msize < (1024 * 1024) ? FileCalculateUtils.calculateFileSizeString(msize.longValue(), FileSizeUnitEnum.KB) : FileCalculateUtils.calculateFileSizeString(msize.longValue(), FileSizeUnitEnum.MB));
                    }
                    map.put("media_size", lsize);
                } catch (Exception e) {
                    System.out.println(" media_size = " + map.get("media_size"));
                    e.printStackTrace();
                }
            }
            Object mediaDuration = map.get("media_duration");
            if (mediaDuration != null) {
                try {
                    Long md = null;
                    String ld = "0";
                    if (mediaDuration instanceof String) {
                        ld = (String) mediaDuration;
                    } else {
                        md = mapValueToLong(mediaDuration);
                        ld = md + "";
                    }
                    map.put("media_duration", ld);
                } catch (Exception e) {
                    System.out.println(" media_duration = " + map.get("media_duration"));
                    e.printStackTrace();
                }
            }
            try {
                List<String> snapshots = new ArrayList<String>();
                Object obj = map.get("media_snapshots");

                if (obj != null) {
                    String mediaSnapshots = (String) obj;

                    String[] mediaSnapshotsArr = mediaSnapshots.split(",");
                    if (mediaSnapshotsArr != null && mediaSnapshotsArr.length > 0) {
                        for (String str : mediaSnapshotsArr) {
                            if (StringUtils.isNotBlank(str)) {
                                snapshots.add(MaterialUtils.getMaterialUrl(str, foreignProperties.getMediaurl()));
                            }
                        }
                    }
                }
                map.put("media_snapshots", StringUtils.join(snapshots, ","));
            } catch (Exception e) {
                System.out.println(" media_snapshots = " + map.get("media_snapshots"));
                e.printStackTrace();
            }
            relist.add(map);
        }
        return relist;
    }

    @Override
    public Integer insertWatermark(EditProjectWatermarkEntity vo) {
        return editProjectWatermarkDao.insert(vo);
    }

    @Override
    public Integer deleteWatermarkByProjectId(String projectId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("project_id", projectId);
        return editProjectWatermarkDao.deleteByMap(map);
    }


    @Override
    public List<HashMap> queryWatermarkList(Map<String, Object> params) {
        String projectId = (String) params.get("project_id");
        String materialType = (String) params.get("material_type");

        List<HashMap> list = editProjectWatermarkDao.selectWatermarkList(params);
        List<HashMap> relist = new ArrayList<HashMap>();
        for (int i = 0; i < list.size(); i++) {
            HashMap map = list.get(i);
            Object cover_url = map.get("cover_url");
            String coverUrl = "";
            if (cover_url != null) {
                coverUrl = (String) cover_url;
            }
            map.put("cover_url", MaterialUtils.getMaterialUrl(coverUrl, foreignProperties.getMediaurl()));
            Object media_url = map.get("media_url");
            String mediaUrl = "";
            if (media_url != null) {
                mediaUrl = (String) media_url;
            }
            map.put("media_url", MaterialUtils.getMaterialUrl(mediaUrl, foreignProperties.getMediaurl()));


            Object mediaSize = map.get("media_size");
            if (mediaSize != null) {
                try {
                    Long msize = null;
                    String lsize = "0";

                    if (mediaSize instanceof String) {
                        lsize = (String) mediaSize;
                    } else {
                        msize = mapValueToLong(mediaSize);
                        lsize = (msize < (1024 * 1024) ? FileCalculateUtils.calculateFileSizeString(msize.longValue(), FileSizeUnitEnum.KB) : FileCalculateUtils.calculateFileSizeString(msize.longValue(), FileSizeUnitEnum.MB));
                    }
                    map.put("media_size", lsize);
                } catch (Exception e) {
                    System.out.println(" media_size = " + map.get("media_size"));
                    e.printStackTrace();
                }
            }
            Object mediaDuration = map.get("media_duration");
            if (mediaDuration != null) {
                try {
                    Long md = null;
                    String ld = "0";
                    if (mediaDuration instanceof String) {
                        ld = (String) mediaDuration;
                    } else {
                        md = mapValueToLong(mediaDuration);
                        ld = md + "";
                    }
                    map.put("media_duration", ld);
                } catch (Exception e) {
                    System.out.println(" media_duration = " + map.get("media_duration"));
                    e.printStackTrace();
                }
            }
            try {
                List<String> snapshots = new ArrayList<String>();
                Object obj = map.get("media_snapshots");

                if (obj != null) {
                    String mediaSnapshots = (String) obj;

                    String[] mediaSnapshotsArr = mediaSnapshots.split(",");
                    if (mediaSnapshotsArr != null && mediaSnapshotsArr.length > 0) {
                        for (String str : mediaSnapshotsArr) {
                            if (StringUtils.isNotBlank(str)) {
                                snapshots.add(MaterialUtils.getMaterialUrl(str, foreignProperties.getMediaurl()));
                            }
                        }
                    }
                }
                map.put("media_snapshots", StringUtils.join(snapshots, ","));
            } catch (Exception e) {
                System.out.println(" media_snapshots = " + map.get("media_snapshots"));
                e.printStackTrace();
            }
            relist.add(map);
        }
        return relist;
    }

    /**
     * 合成成功回调方法
     *
     * @param videoId
     */
    public void produceMediaComplete(String videoId) {
        if (org.apache.commons.lang3.StringUtils.isBlank(videoId)) {
            return;
        }
        LiveMultimediaEntity qentity = new LiveMultimediaEntity();
        LiveMultimediaEntity entity = new LiveMultimediaEntity();
        qentity.setMediaId(videoId);
        qentity = liveMultimediaDao.selectOne(qentity);
        entity.setMediaId(videoId);

        try {
            GetVideoInfoResponse videoInfoResponse = liveClient.liveUploadToVoD().getVideoInfo(videoId);
            if (videoInfoResponse != null) {
                GetVideoInfoResponse.Video video = videoInfoResponse.getVideo();
                GetMezzanineInfoResponse mezzanineInfoResponse = liveClient.liveUploadToVoD().getMezzanineInfo(videoId);
                if (mezzanineInfoResponse != null) {
                    GetMezzanineInfoResponse.Mezzanine mezzanine = mezzanineInfoResponse.getMezzanine();
                    entity.setMediaDuration(video.getDuration() == null ? 0 : video.getDuration());
                    entity.setMediaSize(video.getSize() == null ? 0 : video.getSize());
                    List<String> snapshots = new ArrayList<>();
                    if (video.getSnapshots() != null && video.getSnapshots().size() > 0) {
                        for (String str : video.getSnapshots()) {
                            if (org.apache.commons.lang3.StringUtils.isNotBlank(str)) {
                                snapshots.add(extractString(str, "/", 3));
                            }
                        }
                    }
                    entity.setTitle(video.getTitle());
                    // 20200313_Mxy 视频合成素材类型修改
                    if (StringUtils.isNotBlank(mezzanine.getFileURL())){
                        entity.setMediaType(mezzanine.getFileURL().endsWith(".mp3") ? SceneConstants.MediaType.AUDIO.getValue() : SceneConstants.MediaType.VIDEO.getValue());
                    }
                    entity.setMediaSource(SceneConstants.MediaSource.EDIT);
                    entity.setRequestId(videoInfoResponse.getRequestId());
                    entity.setMediaSnapshots(org.apache.commons.lang3.StringUtils.join(snapshots, ","));
                    entity.setTags(video.getTags());
                    entity.setCoverUrl(extractString(video.getCoverURL(), "/", 3));
                    
                    entity.setCateId((video.getCateId() != null ? video.getCateId() + "" : ""));
                    entity.setMediaUrl(extractString(mezzanine.getFileURL(), "/", 3));
                    entity.setMediaWidth(mezzanine.getWidth() == null ? 0 : mezzanine.getWidth().intValue());
                    entity.setMediaHeight(mezzanine.getHeight() == null ? 0 : mezzanine.getHeight().intValue());
                    entity.setMediaDuration(mezzanine.getDuration() == null ? 0 : Float.parseFloat(mezzanine.getDuration()));
                    entity.setMediaBitrate(mezzanine.getBitrate());
                    entity.setMediaFps(mezzanine.getFps());
                    entity.setCreateDate(new Date());
                    entity.setOriFileName(mezzanine.getFileName());
                    EditProjectEntity epev = new EditProjectEntity();
                    epev.setMaterialId(entity.getMediaId());
                    if(StringUtils.isNotBlank(entity.getMediaUrl()) && !entity.getMediaUrl().endsWith(".m3u8")){
                    	entity.setState(SceneConstants.MediaState.Normal.value());
                    }
                    EditProjectEntity pvo = editProjectDao.selectOne(epev);
                    //如果为空的话说明没有项目，只是素材
                    if (pvo == null) {

                    } else {
                        pvo.setStatus("Produced");
                        updateProjectByProjectId(pvo);
                        entity.setTenantId(pvo.getTenantId());
                        try {
                            if (pvo.getCreateUser() != null && pvo.getCreateUser() > 0)
                                entity.setCreatorId(Long.valueOf(pvo.getCreateUser() + ""));
                        } catch (Exception e) {

                        }

                    }
                    if (qentity != null && qentity.getId() != null) {
                        liveMultimediaDao.updateById(entity);
                    } else {
                        liveMultimediaDao.insert(entity);
                    }
                }
            }
        } catch (Exception e) {
            entity.setState(SceneConstants.MediaState.UploadFail.value());
            e.printStackTrace();
        }

    }

    private String extractString(String oriStr, String subStr, int i) {
        if (StringUtils.isBlank(oriStr) || StringUtils.isBlank(subStr) || i < 0) {
            return null;
        }
        for (int j = 0; j < i; j++) {
            if (j == i - 1) {
                oriStr = oriStr.substring(oriStr.indexOf(subStr));
            } else {
                oriStr = oriStr.substring(oriStr.indexOf(subStr) + 1);
            }
        }
        return oriStr;
    }

    public Long mapValueToLong(Object obj) {
        Long reValue = null;
        if (obj instanceof Float) {
            Float fd = (Float) obj;
            reValue = fd.longValue();
        } else if (obj instanceof Long) {
            reValue = (Long) obj;
        } else if (obj instanceof Integer) {
            Integer ivalue = (Integer) obj;
            reValue = ivalue.longValue();
        }
        return reValue;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(EditProjectMaterialEntity video) {
        this.insert(video);
    }


    @Override
    public AddEditingProjectResponse addEditProject(AddEditingProjectRequest request) {
        AddEditingProjectResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public UpdateEditingProjectResponse updateEditingProject(UpdateEditingProjectRequest request) {
        UpdateEditingProjectResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public GetEditingProjectResponse getEditingProject(GetEditingProjectRequest request) {
        GetEditingProjectResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public DeleteEditingProjectResponse deleteEditingProject(DeleteEditingProjectRequest request) {
        DeleteEditingProjectResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public SearchEditingProjectResponse searchEditingProject(SearchEditingProjectRequest request) {
        SearchEditingProjectResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }


    @Override
    public SetEditingProjectMaterialsResponse setEditingProjectMaterials(SetEditingProjectMaterialsRequest request) {
        SetEditingProjectMaterialsResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public AddWatermarkResponse setAddWatermarkRequest(AddWatermarkRequest request) {
        AddWatermarkResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public <T extends AcsResponse> T request(AcsRequest<T> request) {
        T response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }


    @Override
    public GetEditingProjectMaterialsResponse getEditingProjectMaterials(GetEditingProjectMaterialsRequest request) {
        GetEditingProjectMaterialsResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public ProduceEditingProjectVideoResponse produceEditingProjectVideo(ProduceEditingProjectVideoRequest request) {
        ProduceEditingProjectVideoResponse response = null;

        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public GetVideoInfoResponse getVideoInfo(GetVideoInfoRequest request) {
        GetVideoInfoResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public UpdateVideoInfoResponse updateVideoInfo(UpdateVideoInfoRequest request) {
        UpdateVideoInfoResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public DeleteVideoResponse deleteVideo(DeleteVideoRequest request) {
        DeleteVideoResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public GetVideoListResponse getVideoList(GetVideoListRequest request) {
        GetVideoListResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public GetVideoPlayAuthResponse getVideoPlayAuth(GetVideoPlayAuthRequest request) {
        GetVideoPlayAuthResponse response = null;
        try {
            request.setAuthInfoTimeout(3000L);
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }


    @Override
    public ProduceEditingProjectVideoResponse produceEditProject(ProduceEditingProjectVideoRequest request) {
        ProduceEditingProjectVideoResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public ProduceEditingProjectVideoResponse clipVideo(String videoId, Float duration) {
        String timeLine = "{\"VideoTracks\":[{\"Count\":1," +
                "\"VideoTrackClips\":[{\"index\":0," +
                "\"VideoId\":\"" + videoId + "\"," +
                "\"Out\":" + duration + "," +
                "\"Duration\":" + duration + "," +
                "\"width\":0," +
                "\"In\":0," +
                "\"height\":0}]}]}";
        ProduceEditingProjectVideoRequest request = new ProduceEditingProjectVideoRequest();
        request.setTimeline(timeLine);
        return produceEditProject(request);
    }
}
