package cn.com.ctop.toutiao.modules.adlab.controller;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.toutiao.modules.adlab.service.ICreateProjectService;
import cn.com.ctop.toutiao.modules.adlab.service.ICtopAdlabProjectDetailService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("/adlab/Project")
public class CreateAdlabController {

    @Autowired
    ICreateProjectService createProjectService;
    @Autowired
    ICtopOauthTokenService ctopOauthTokenService;
    @Autowired
    ICtopAdlabProjectDetailService adlabProjectDetailService;


    /**
     * 投放管家创建项目第一步
     */
    @PostMapping(value = "/create")
    public Result<List<JSONObject>> create(@RequestBody JSONObject requestBody) {
        Result<List<JSONObject>> result = new Result<>();
        JSONArray accountIds = requestBody.getJSONArray("accountIds");
        if (accountIds.isEmpty()) {
            result.error500("请选择账户再继续操作");
        } else {
            List<JSONObject> resultList = new ArrayList<>();
            accountIds.forEach(accountId -> {
                //临时存每一个账户创建后的数据
                JSONObject repoTemp = new JSONObject();
                requestBody.put("accountId", accountId);
                //查询该账户的token
                CtopOauthToken token = ctopOauthTokenService.getTokenByAccountId((Long) accountId);
                JSONObject repo = createProjectService.createGroup(token, requestBody);
                if (repo.getInteger("code") == 0) {
                    repoTemp.put("code", 0);
                    repoTemp.put("accountId", accountId);
                    repoTemp.put("groupId", repo.getJSONObject("data").getLong("group_id"));
                    //在此塞进去，前端后续会用来做判断条件
                    repoTemp.put("name", requestBody.getString("name"));
                    repoTemp.put("adTarget", requestBody.getString("adTarget"));
                    repoTemp.put("deliveryRange", requestBody.getString("deliveryRange"));
                    repoTemp.put("landingType", requestBody.getString("landingType"));
                } else {
                    repoTemp.put("code", 500);
                    repoTemp.put("message", repo.getString("message"));
                    repoTemp.put("accountId", accountId);
                }
                resultList.add(repoTemp);
            });
            result.setResult(resultList);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 投放管家创建项目第二步，设置项目定向信息
     */
    @PostMapping(value = "/setConvert")
    public Result<List<JSONObject>> setConvert(@RequestBody JSONArray requestBody) {
        Result<List<JSONObject>> result = new Result<>();
        List<JSONObject> resultList = new ArrayList<>();
        if (requestBody.size() > 0) {
            requestBody.forEach(convert -> {
                Map<String, Object> convert2Map = (Map<String, Object>) convert;
                JSONObject repoTemp = new JSONObject();
                CtopOauthToken token = ctopOauthTokenService.getTokenByAccountId((Long) convert2Map.get("accountId"));
                JSONObject repo = createProjectService.updateGroupConvert(token, convert2Map);
                if (repo.getInteger("code") == 0) {
                    repoTemp.put("code", 0);
                    repoTemp.put("accountId", convert2Map.get("accountId"));
                    repoTemp.put("groupId", repo.getJSONObject("data").getLong("group_id"));
                    repoTemp.put("name", convert2Map.get("name"));
                    repoTemp.put("adTarget", convert2Map.get("adTarget"));
                    repoTemp.put("deliveryRange", convert2Map.get("deliveryRange"));
                    repoTemp.put("landingType", convert2Map.get("landingType"));
                } else {
                    repoTemp.put("code", 500);
                    repoTemp.put("message", repo.getString("message"));
                    repoTemp.put("accountId", convert2Map.get("accountId"));
                }
                resultList.add(repoTemp);
            });
            result.setResult(resultList);
            result.setSuccess(true);
        } else {
            result.error500("请选择需要设置转化信息的项目");
        }
        return result;
    }

    /**
     * 投放管家创建项目第三步，设置项目定向信息
     */
    @PostMapping(value = "/setAudience")
    public Result<List<JSONObject>> setAudience(@RequestBody JSONArray requestBody) {

        Result<List<JSONObject>> result = new Result<>();
        List<JSONObject> resultList = new ArrayList<>();
        if (requestBody.size() > 0) {
            requestBody.forEach(convert -> {
                Map<String, Object> convert2Map = (Map<String, Object>) convert;
                JSONObject repoTemp = new JSONObject();
                CtopOauthToken token = ctopOauthTokenService.getTokenByAccountId((Long) convert2Map.get("accountId"));
                JSONObject repo = createProjectService.updateGroupAudience(token, convert2Map);
                if (repo.getInteger("code") == 0) {
                    repoTemp.put("code", 0);
                    repoTemp.put("accountId", convert2Map.get("accountId"));
                    repoTemp.put("groupId", repo.getJSONObject("data").getLong("group_id"));
                    repoTemp.put("name", convert2Map.get("name"));
                    repoTemp.put("adTarget", convert2Map.get("adTarget"));
                    repoTemp.put("deliveryRange", convert2Map.get("deliveryRange"));
                    repoTemp.put("landingType", convert2Map.get("landingType"));
                    repoTemp.put("inventoryType", convert2Map.get("inventoryType"));
                } else {
                    repoTemp.put("code", 500);
                    repoTemp.put("message", repo.getString("message"));
                    repoTemp.put("accountId", convert2Map.get("accountId"));
                }
                resultList.add(repoTemp);
            });
            result.setResult(resultList);
            result.setSuccess(true);
        } else {
            result.error500("请选择需要设置定向信息的项目");
        }
        return result;
    }

    /**
     * 投放管家创建项目第四步，设置项目创意信息
     */
    @PostMapping(value = "/setCreative")
    public Result<List<JSONObject>> setCreative(@RequestBody JSONArray requestBody) {
        Result<List<JSONObject>> result = new Result<>();
        List<JSONObject> resultList = new ArrayList<>();
        if (requestBody.size() > 0) {
            requestBody.forEach(convert -> {
                Map<String, Object> convert2Map = (Map<String, Object>) convert;
                JSONObject repoTemp = new JSONObject();
                CtopOauthToken token = ctopOauthTokenService.getTokenByAccountId((Long) convert2Map.get("accountId"));
                JSONObject repo = createProjectService.updateGroupCreative(token, convert2Map);

                if (repo.getInteger("code") == 0) {
                    repoTemp.put("code", 0);
                    repoTemp.put("accountId", convert2Map.get("accountId"));
                    repoTemp.put("groupId", repo.getJSONObject("data").getLong("group_id"));
                    repoTemp.put("name", convert2Map.get("name"));
                    repoTemp.put("adTarget", convert2Map.get("adTarget"));
                    repoTemp.put("deliveryRange", convert2Map.get("deliveryRange"));
                    repoTemp.put("landingType", convert2Map.get("landingType"));
                    repoTemp.put("inventoryType", convert2Map.get("inventoryType"));
                } else {
                    repoTemp.put("code", 500);
                    repoTemp.put("message", repo.getString("message"));
                    repoTemp.put("accountId", convert2Map.get("accountId"));
                }
                resultList.add(repoTemp);
            });
            result.setResult(resultList);
            result.setSuccess(true);
        } else {
            result.error500("请选择需要设置定向信息的项目");
        }
        return result;
    }

    /**
     * 投放管家创建项目第五步，设置项目预算信息
     */
    @PostMapping(value = "/setBudget")
    public Result<List<JSONObject>> setBudget(@RequestBody JSONArray requestBody) {

        Result<List<JSONObject>> result = new Result<>();
        List<JSONObject> resultList = new ArrayList<>();
        if (requestBody.size() > 0) {
            requestBody.forEach(convert -> {
                Map<String, Object> convert2Map = (Map<String, Object>) convert;
                JSONObject repoTemp = new JSONObject();
                CtopOauthToken token = ctopOauthTokenService.getTokenByAccountId((Long) convert2Map.get("accountId"));
                JSONObject repo = createProjectService.updateGroupBudget(token, convert2Map);
                log.info(repo.toJSONString());
                if (repo.getInteger("code") == 0) {
                    repoTemp.put("code", 0);
                    repoTemp.put("accountId", convert2Map.get("accountId"));
                    repoTemp.put("groupId", repo.getJSONObject("data").getLong("group_id"));
                    repoTemp.put("userId", convert2Map.get("userId"));
                } else {
                    repoTemp.put("code", 500);
                    repoTemp.put("message", repo.getString("message"));
                    repoTemp.put("accountId", convert2Map.get("accountId"));
                }
                resultList.add(repoTemp);
            });
            //入库
            adlabProjectDetailService.putStorage(resultList);
            result.setResult(resultList);
            result.setSuccess(true);
        } else {
            result.error500("请选择需要设置定向信息的项目");
        }
        return result;
    }

}
