package org.jeecg.modules.ctop.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.ctop.service.ICreativeTitleService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CreativeTitleServiceImpl implements ICreativeTitleService {
    @Override
    public JSONArray getTitleLabel() {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        String result = HttpUtils.httpGet("https://cc.oceanengine.com/creative_center_server/api/title/new_title_label", null, headers);
        JSONObject resultJson = JSONObject.parseObject(result);
        if (resultJson.getInteger("code") == 0) {
            JSONObject dataJson = resultJson.getJSONObject("data");
            if (!Check.isNull(dataJson)) {
                return dataJson.getJSONArray("title_label");
            }
        }
        return null;
    }

    @Override
    public JSONArray titleSuggestion(String firstCategory, String secondCategory, String keyword) {
        if (Check.isNull(keyword)) {
            return null;
        }

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("first_category", firstCategory);
        paramsMap.put("second_category", secondCategory);
        paramsMap.put("keyword", keyword);

        String result = HttpUtils.httpGet("https://cc.oceanengine.com/creative_center_server/api/title/title_suggestion", paramsMap, headers);
        JSONObject resultJson = JSONObject.parseObject(result);
        if (resultJson.getInteger("code") == 0) {
            JSONObject dataJson = resultJson.getJSONObject("data");
            if (!Check.isNull(dataJson)) {
                return dataJson.getJSONArray("titles");
            }
        }
        return null;
    }

    @Override
    public JSONArray titleSearch(String firstCategory, String secondCategory, String keywords) {
        if (Check.isNull(keywords)) {
            return null;
        }
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("first_category", firstCategory);
        paramsMap.put("second_category", secondCategory);
        JSONObject json = new JSONObject();
        json.put("keywords", keywords);
        paramsMap.put("params", json);
        String result = HttpUtils.httpGet("https://cc.oceanengine.com/creative_center_server/api/title/title_search", paramsMap, headers);
        JSONObject resultJson = JSONObject.parseObject(result);
        if (resultJson.getInteger("code") == 0) {
            JSONObject dataJson = resultJson.getJSONObject("data");
            if (!Check.isNull(dataJson)) {
                return dataJson.getJSONArray("titles");
            }
        }
        return null;
    }
}
