package com.platform.autoconfigure.live;

import com.platform.plugs.live.config.*;
import com.platform.plugs.common.rpc.DefaultRequestParams;
import com.platform.plugs.common.rpc.Profile;
import com.platform.plugs.live.LiveClient;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author TONG
 * @Date 2019/2/18 14:23
 **/
@Configuration
@ConditionalOnClass({LiveClient.class})
@EnableConfigurationProperties({LiveProperties.class})
public class LiveAutoConfiguration {

    private final LiveProperties properties;

    private LiveClient liveClient;

    public LiveAutoConfiguration(LiveProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public LiveClient liveClient() {
        this.liveClient = createLiveClient();
        return liveClient;
    }

    private LiveClient createLiveClient() {

        this.liveClient = new LiveClient(
                new Profile(properties.getCommon().getRegionId(), properties.getAccount().getAccessKeyId(), properties.getAccount().getSecret()),
                new DefaultRequestParams(properties.getCommon().getDomain(), properties.getCommon().getVersion()));

        LiveProperties.LiveRecord liveRecord = properties.getRecord();
//        LiveRecordConfig liveRecordConfig = new LiveRecordConfig(
//                liveRecord.getDomainName(), liveRecord.getOssBucket(), liveRecord.getOssEndpoint(),
//                liveRecord.getRecordFormat().getRecordFormat(), liveRecord.getRecordFormat().getOssRecordPrefix() + liveRecord.getRecordFormat().getOssObjectPrefix(), null,
//                liveRecord.getOnDemand(), liveRecord.getRecordFormat().getCycleDuration());
        LiveRecordConfig liveRecordConfig = new LiveRecordConfig(
                liveRecord.getDomainName(), liveRecord.getVodTranscodeGroupIdMp4(), liveRecord.getVodTranscodeGroupIdMp3(), liveRecord.getVodRecordCategory(), liveRecord.getRecordFormat().getCycleDuration());
        this.liveClient.configLiveRecord(liveRecordConfig);

        LiveProperties.LiveDomains domains = properties.getDomains();
        LiveStreamConfig liveStreamConfig = new LiveStreamConfig(
                domains.getPush().getDomainName(), domains.getPush().getKey(),
                domains.getLive().getDomainName(), domains.getLive().getKey());
        if(domains.getPush().getAuthTime() != null)
            liveStreamConfig.setPushAuthTime(domains.getPush().getAuthTime());
        if(domains.getLive().getAuthTime() != null)
            liveStreamConfig.setLiveAuthTime(domains.getLive().getAuthTime());
        this.liveClient.configLiveStream(liveStreamConfig);

        LiveProperties.LiveCaster caster = properties.getCaster();
        String ossObjectPrefix = liveRecord.getRecordFormat().getOssRecordPrefix() + (liveRecord.getRecordFormat().getOssObjectPrefix().replaceAll("Escaped", ""));
        LiveCasterConfig liveCasterConfig = new LiveCasterConfig(
                domains.getLive().getDomainName(), caster.getCasterTemplate(), caster.getLiveTemplateIds(),
                liveRecord.getOssBucket(), liveRecord.getOssEndpoint(), liveRecord.getRecordFormat().getRecordFormat(),
                ossObjectPrefix, null, liveRecord.getRecordFormat().getCycleDuration());
        this.liveClient.configLiveCaster(liveCasterConfig);
        
        LiveUploadToVoDConfig liveUploadToVoDConfig = new LiveUploadToVoDConfig(properties.getAccount().getAccessKeyId(),properties.getAccount().getSecret(),domains.getMedia().getDomainName(), domains.getMedia().getBucket(), domains.getMedia().getEndpoint(), domains.getMedia().getApiRegionId());
        this.liveClient.configLiveUploadToVoD(liveUploadToVoDConfig);

        LiveTransCodeConfig liveTransCodeConfig = new LiveTransCodeConfig(properties.getAccount().getAccessKeyId(), properties.getAccount().getSecret(), properties.getCommon().getRegionId());
        this.liveClient.configLiveTransCode(liveTransCodeConfig);

//        LiveAudioConfig liveAudioConfig = new LiveAudioConfig(properties.getAudio().getRecognition().getAppKey(), properties.getAudio().getRecognition().getRegionId(), properties.getAudio().getRecognition().getEndpointName(),
//        		properties.getAudio().getRecognition().getProduct(), properties.getAudio().getRecognition().getDomainName(), properties.getAudio().getRecognition().getApiVersion(), properties.getAudio().getRecognition().getPostRequestAction(),
//        		properties.getAudio().getRecognition().getGetRequestAction(), properties.getAccount().getAccessKeyId(), properties.getAccount().getSecret(), properties.getAudio().getRecognition().getCallbackDomainName());
//        this.liveClient.configLiveAudio(liveAudioConfig);

        LivePullStreamConfig livePullStreamConfig = new LivePullStreamConfig(domains.getLive().getDomainName(), properties.getAccount().getAccessKeyId(),properties.getAccount().getSecret(), properties.getCommon().getRegionId());
        this.liveClient.configLivePullStream(livePullStreamConfig);
        
        return this.liveClient;
    }
}
