package com.zcloud.service.gateway;

import com.alibaba.fastjson.JSONObject;
import com.zcloud.entity.PageData;
import com.zcloud.util.UuidUtil;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class Warden {
    public static List<PageData> createdCurriculumModel(String chapterList, String id) {
        List list = JSONObject.parseObject(chapterList, List.class);
        List<PageData> result = new ArrayList<>();
        for (Object x : list) {
            JSONObject _x = JSONObject.parseObject(x.toString());
            PageData y = new PageData();
            String CHAPTER_ID = get32UUID();
            y.put("CHAPTER_ID",get32UUID());
            y.put("CHAPTER_ID_REMOTE",_x.getString("chapter_id"));
            y.put("SORT",_x.getString("sort"));
            y.put("NAME",_x.getString("name"));
            y.put("PARENT_ID",_x.getString("parent_id"));
            y.put("VIDEOCOURSEWARE_ID",_x.getString("video_courseware_id"));
            y.put("CURRICULUM_ID",id);
            y.put("VIDEOTIME",_x.getString("video_time"));
            y.put("COURSEWARENAME",_x.getString("courseware_name"));
            y.put("ISDELETE","0");
            result.add(y);
            if (StringUtils.isNotBlank(_x.getString("node")) && "null".equals(_x.getString("node"))) {
                List<String> son_list = JSONObject.parseObject(_x.getString("node"), List.class);
                for (String _m : son_list) {
                    JSONObject m =  JSONObject.parseObject(_m);
                    PageData _y = new PageData();
                    _y.put("CHAPTER_ID",get32UUID());
                    _y.put("CHAPTER_ID_REMOTE",m.getString("chapter_id"));
                    _y.put("SORT",m.getString("sort"));
                    _y.put("NAME",m.getString("name"));
                    _y.put("PARENT_ID",m.getString("parent_id"));
                    _y.put("VIDEOCOURSEWARE_ID",m.getString("video_courseware_id"));
                    _y.put("VIDEOTIME",_x.getString("video_time"));
                    _y.put("CURRICULUM_ID",id);
                    _y.put("COURSEWARENAME",m.getString("courseware_name"));
                    _y.put("ISDELETE","0");
                    result.add(_y);
                }
            }
        }
        return result;
    }

    public static List<PageData> createVideoModel(String chapterList, String id) {
        List<JSONObject> list = JSONObject.parseObject(chapterList, List.class);
        List<PageData> result = new ArrayList<>();
        for (JSONObject x : list) {
            JSONObject _x = JSONObject.parseObject(x.toJSONString());
            if (StringUtils.isNotBlank(_x.getString("video_courseware_id"))){
                PageData y = new PageData();
                y.put("VIDEOCOURSEWARE_ID_REMOTE",_x.getString("video_courseware_id"));
                y.put("VIDEOCOURSEWARE_ID", get32UUID());
                y.put("VIDEOTIME",_x.getString("video_time"));
                y.put("ISDELETE","0");
                result.add(y);
            }
            if (StringUtils.isNotBlank(_x.getString("node")) && "null".equals(_x.getString("node"))) {
                List<String> son_list = JSONObject.parseObject(_x.getString("node"), List.class);
                for (String _m : son_list) {
                    JSONObject m =  JSONObject.parseObject(_m);
                    PageData _y = new PageData();
                    _y.put("VIDEOCOURSEWARE_ID_REMOTE",_x.getString("video_courseware_id"));
                    _y.put("VIDEOCOURSEWARE_ID", get32UUID());
                    _y.put("VIDEOTIME",_x.getString("video_time"));
                    _y.put("ISDELETE","0");
                    result.add(_y);
                }
            }
        }
        return result;
    }


    public static String get32UUID() {
        return UuidUtil.get32UUID();
    }
}
