package com.zcloud.service.gateway.impl;

import com.alibaba.fastjson.JSONObject;
import com.zcloud.entity.PageData;
import com.zcloud.service.gateway.SynchronizeInformationService;
import com.zcloud.service.gateway.Warden;
import com.zcloud.service.onlinexxks.ChapterService;
import com.zcloud.service.onlinexxks.CurriculumService;
import com.zcloud.service.onlinexxks.VideoCoursewareService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.HttpRequestUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.MD5;
//import com.zcloud.vo.CurricullumInformation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SynchronizeInformationServiceImpl implements SynchronizeInformationService {

    @Value("${customer.id}")
    private String customerId;

    @Value("${customer.SECRET_KEY}")
    private String customer_secret_key;

    @Value("${customer.url}")
    private String url;

    @Value("${customer.domain}")
    private String domain;

    @Resource
    private ChapterService chapterService;

    @Resource
    private VideoCoursewareService videoCoursewareService;

    @Resource
    private CurriculumService curriculumService;

    /**
     * description: 获取视频播放链接接口<p/>
     *
     * @throws Exception
     */
//    @Override
//    public VideoInformation syncVideoInformationByChapter(PageData _video) throws Exception {
//        PageData chapter = chapterService.findById(_video);
//        if (chapter == null || chapter.size() <= 0) {
//            throw new RuntimeException("未找到对应的章节");
//        }
//        return syncVideoInformationByVideoId(chapter);
//    }

    /*
     * description: 获取视频播放链接接口<p/>
     *
     * @throws Exception
     */
    @Override
    public PageData syncVideoPlayAuth(PageData video) throws Exception {
//        PageData video = videoCoursewareService.findById(_video);
//        if (video == null || video.size() <= 0) {
//            throw new RuntimeException("未找到对应的视频");
//        }
        HashMap<String, String> request = new HashMap<>();
        request.put("id", this.customerId);
        request.put("videoId", video.getString("VIDEOCOURSEWARE_ID"));
        request.put("curriculumId", video.getString("CURRICULUM_ID"));
        request.put("type", "0");
        String key = MD5.md5(this.customer_secret_key + domain);
        request.put("SECRET_KEY", key);
        String _response = HttpRequestUtil.doPost(url + "/videoCourseware/getPlayInfo", JSONObject.toJSONString(request));
        PageData response = JSONObject.parseObject(_response, PageData.class);
        return response;
    }

    /**
     * description: 获取视频封面<p/>
     *
     * @throws Exception
     */
    @Override
    public PageData syncVideoInfo(PageData video) throws Exception {
//        PageData video = videoCoursewareService.findById(_video);
//        if (video == null || video.size() <= 0) {
//            throw new RuntimeException("未找到对应的视频");
//        }
        HashMap<String, String> request = new HashMap<>();
        request.put("id", this.customerId);
        request.put("videoId", video.getString("VIDEOCOURSEWARE_ID_REMOTE"));
        request.put("curriculumId", video.getString("CURRICULUM_ID"));
        request.put("type", "0");
        String key = MD5.md5(this.customer_secret_key + domain);
        request.put("SECRET_KEY", key);
        String _response = HttpRequestUtil.doPost(url + "/videoCourseware/getVideoInfo", JSONObject.toJSONString(request));
        PageData response = JSONObject.parseObject(_response, PageData.class);
        return response;
    }


    @Override
    @Transactional
    public void syncCurriculum(List<String> list) throws Exception {
//        HashMap<String, Object> request = new HashMap<>();
//        request.put("id", this.customerId);
//        String key = MD5.md5(this.customer_secret_key + domain);
//        request.put("SECRET_KEY", key);
//        request.put("curriculumId", list);
//        String _response = HttpRequestUtil.doPost(url + "/getClass", JSONObject.toJSONString(request));
//        CurricullumInformation curricullumInformation = JSONObject.parseObject(_response, CurricullumInformation.class);
//        if (!curricullumInformation.code.equals("0")) {
//            throw new RuntimeException(curricullumInformation.getErrorMessage());
//        }
//        for (String x : list) {
//            JSONObject vectory = JSONObject.parseObject(curricullumInformation.getCurriculum_information().get(x));
//            PageData pd = new PageData();
//            pd.put("CURRICULUM_ID_REMOTE", x);
//            String id = Warden.get32UUID();
//            pd.put("CURRICULUM_ID", id);
//            pd.put("CLASSHOUR", vectory.getString("class_hour"));
//            pd.put("CURRICULUMNAME", vectory.getString("curriculum_name"));
//            pd.put("CURRICULUMINTRODUCE", vectory.getString("curriculum_introduce"));
//            pd.put("ISDELETE", "0");
//            pd.put("CREATOR", Jurisdiction.getUsername());	//添加人
//            pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
//            pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
//            pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
//            pd.put("ISCOMPLETE", "0");
//            pd.put("STATE", "1");
//            // TODO 这里要做重复校验
//            curriculumService.save(pd);
//            List<PageData> video = Warden.createVideoModel(vectory.getString("chapterList"), id);
//            for (PageData m : video) {
//                m.put("CREATOR", Jurisdiction.getUsername());	//添加人
//                m.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
//                m.put("OPERATOR", Jurisdiction.getUsername());	//修改人
//                m.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
//                m.put("STATE", "0");
//                m.put("ISCOMPLETE", "0");
//                videoCoursewareService.save(m);
//            }
//            List<PageData> chapter = Warden.createdCurriculumModel(vectory.getString("chapterList"), id);
//            for (PageData y : chapter) {
//                List<PageData> _list = video.stream()
//                        .filter(n -> n.getString("VIDEOCOURSEWARE_ID_REMOTE").equals(y.getString("VIDEOCOURSEWARE_ID")))
//                        .collect(Collectors.toList());
//                if (_list != null || _list.size() >= 0) {
//                    throw new RuntimeException("数据结构异常");
//                }
//                y.put("VIDEOCOURSEWARE_ID", _list.get(0).getString("VIDEOCOURSEWARE_ID"));
//                chapterService.save(y);
//            }
//        }
    }
}
