package com.tuoheng.admin.service.aliyun;

import com.aliyun.oss.ClientException;
import com.aliyun.tea.TeaModel;
import com.aliyun.vod20170321.Client;
import com.aliyun.vod20170321.models.GetPlayInfoRequest;
import com.aliyun.vod20170321.models.ListLiveRecordVideoRequest;
import com.aliyun.vod20170321.models.ListLiveRecordVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.tuoheng.admin.config.AliyuncsVodConfig;
import com.tuoheng.admin.service.aliyun.IAliyuncsVodService;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class AliyuncsVodServiceImpl implements IAliyuncsVodService {

    @Autowired
    @Qualifier("vodClient")
    private Client vodClient;

    //填入AccessKey信息
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }


    @Override
    public CreateUploadVideoResponse createUploadVideo(String title, String fileName) throws ClientException {
        CreateUploadVideoResponse response = new CreateUploadVideoResponse();
        try {
            DefaultAcsClient client = initVodClient(AliyuncsVodConfig.accessKeyId, AliyuncsVodConfig.accessKeySecret);

            CreateUploadVideoRequest request = new CreateUploadVideoRequest();
            request.setTitle(title);
            request.setFileName(fileName);
            response = client.getAcsResponse(request);

            log.info("VideoId = " + response.getVideoId());
            log.info("UploadAddress = " + response.getUploadAddress());
            log.info("UploadAuth = " + response.getUploadAuth());
            log.info("RequestId = " + response.getRequestId());
        } catch (Exception e) {
            log.error("ErrorMessage = " + e.getLocalizedMessage());
        }
        return response;
    }

    @Override
    public RefreshUploadVideoResponse refreshUploadVideo(String videoId) {
        RefreshUploadVideoResponse response = new RefreshUploadVideoResponse();
        try {
            DefaultAcsClient client = initVodClient(AliyuncsVodConfig.accessKeyId, AliyuncsVodConfig.accessKeySecret);

            RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
            //音频或视频ID
            request.setVideoId(videoId);
            response = client.getAcsResponse(request);

            log.info("VideoId = " + response.getVideoId());
            log.info("UploadAddress = " + response.getUploadAddress());
            log.info("UploadAuth = " + response.getUploadAuth());
            log.info("RequestId = " + response.getRequestId());

        } catch (Exception e) {
            log.error("ErrorMessage = " + e.getLocalizedMessage());
        }
        return response;

    }

    @Override
    public String getVodVideoId(String appName, String domainName, String streamName) {
        try {
            ListLiveRecordVideoRequest listLiveRecordVideoRequest = new ListLiveRecordVideoRequest();
            listLiveRecordVideoRequest.setAppName(appName);
            listLiveRecordVideoRequest.setDomainName(domainName);
            listLiveRecordVideoRequest.setStreamName(streamName);
            listLiveRecordVideoRequest.setPageNo(1);
            listLiveRecordVideoRequest.setPageSize(1);
            ListLiveRecordVideoResponse resp = vodClient.listLiveRecordVideo(listLiveRecordVideoRequest);
            String vodVideoId = (String)((Map)((Map)((List)((Map) ((Map) TeaModel.buildMap(resp).get("body")).get("LiveRecordVideoList"))
                    .get("LiveRecordVideo")).get(0)).get("Video")).get("VideoId");
            return vodVideoId;
        } catch (Exception e){
            log.error("获取vod视频videoId失败", e);
        }
        return null;
    }

    @Override
    public String getVodVideoUrl(String vodVideoId) {
        try {
            if(StringUtils.isEmpty(vodVideoId)){
                return null;
            }
            GetPlayInfoRequest getPlayInfoRequest = new GetPlayInfoRequest();
            getPlayInfoRequest.setVideoId(vodVideoId);
            Map<String, Object> result = TeaModel.buildMap(vodClient.getPlayInfo(getPlayInfoRequest));
            return (String)((Map)((List)((Map)((Map) result.get("body")).get("PlayInfoList")).get("PlayInfo"))
                    .get(0)).get("PlayURL");
        } catch (Exception e){
            log.error("获取vod视频结果失败", e);
        }
        return null;
    }

    @Override
    public GetVideoInfoResponse getVideoInfo(String videoId) {
        DefaultAcsClient client = initVodClient(AliyuncsVodConfig.accessKeyId, AliyuncsVodConfig.accessKeySecret);
        GetVideoInfoResponse response = new GetVideoInfoResponse();
        try {
            GetVideoInfoRequest request = new GetVideoInfoRequest();
            request.setVideoId(videoId);
            response = client.getAcsResponse(request);
            log.info("Title = " + response.getVideo().getTitle());
            log.info("Description = " + response.getVideo().getDescription());
            log.info("Duration = " + response.getVideo().getDuration());
        } catch (Exception e) {
            log.error("获取视频信息失败", e);
        }
        return response;
    }

}
