package com.zanxiang.game.module.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zanxiang.erp.security.annotation.PreAuthorize;
import com.zanxiang.game.module.base.pojo.enums.AccountTypeEnum;
import com.zanxiang.game.module.manage.pojo.dto.TencentNewWebUserActionSetDTO;
import com.zanxiang.game.module.manage.pojo.params.AgentAddParam;
import com.zanxiang.game.module.manage.pojo.params.AgentListParam;
import com.zanxiang.game.module.manage.pojo.vo.AgentVO;
import com.zanxiang.game.module.manage.service.IAgentService;
import com.zanxiang.game.module.manage.service.IUserAgentLogService;
import com.zanxiang.game.module.mybatis.entity.Agent;
import com.zanxiang.module.util.exception.BaseException;
import com.zanxiang.module.util.pojo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/agent")
public class AgentController {

    @Autowired
    private IAgentService agentService;

    @Autowired
    private IUserAgentLogService userAgentLogService;

    @PreAuthorize(permissionKey = "manage:agent:list")
    @PostMapping("/list")
    public ResultVO<IPage<AgentVO>> listOfPage(@Validated @RequestBody AgentListParam dto) {
        return ResultVO.ok(agentService.listOfPage(dto));
    }

    @PreAuthorize(permissionKey = "manage:agent:newWebUserActionSet")
    @PostMapping("/newWebUserActionSet")
    public ResultVO<Boolean> newWebUserActionSet(@Validated @RequestBody TencentNewWebUserActionSetDTO dto) {
        return ResultVO.ok(agentService.newWebUserActionSet(dto));
    }

    @PreAuthorize(permissionKey = "manage:agent:add")
    @PostMapping
    public ResultVO<Boolean> add(@Validated @RequestBody AgentAddParam dto) {
        dto.setAgentName(dto.getAgentName().trim());
        AccountTypeEnum accountType = AccountTypeEnum.getByValue(dto.getAccountType());
        if (accountType == null) {
            throw new BaseException("错误的账号类型：" + dto.getAccountType());
        }
        if (accountType == AccountTypeEnum.UN_PUT) {
            dto.setAccountId(null);
        } else {
            if (dto.getAccountId() == null) {
                throw new BaseException("广告账号不能为空！");
            }
        }
        if (accountType == AccountTypeEnum.TENCENT_H5) {
            if (dto.getUserActionSetId() == null) {
                throw new BaseException("腾讯广告账号必须选回传的数据源");
            }
        } else if (accountType == AccountTypeEnum.BYTE) {
            if (StringUtils.isBlank(dto.getReportToken()) || StringUtils.isBlank(dto.getReportUrl())) {
                throw new BaseException("头条广告账号必须填入回传的 token 和 url");
            }
            dto.setReportToken(dto.getReportToken().trim());
            dto.setReportUrl(dto.getReportUrl().trim());
        } else if (accountType == AccountTypeEnum.TENCENT_APP) {
            if (dto.getUserActionSetId() == null) {
                throw new BaseException("腾讯广告账号必须选回传的数据源");
            }
        } else if (accountType == AccountTypeEnum.BYTE_APP) {
            //
        }
        if (dto.getPutStatus() == null) {
            dto.setPutStatus(Agent.PUT_STATUS_ING);
        }
        return ResultVO.ok(agentService.add(dto));
    }

    @PreAuthorize(permissionKey = "manage:agent:configPutStatus")
    @PutMapping("/configPutStatus/{agentIds}/{putStatus}")
    public ResultVO<Boolean> configPutStatus(@PathVariable("agentIds") List<Long> agentIds, @PathVariable("putStatus") Integer putStatus) {
        return ResultVO.ok(agentService.configPutStatus(agentIds, putStatus));
    }

    @PreAuthorize(permissionKey = "manage:agent:configBackPolicy")
    @PutMapping({"/configBackPolicy/{agentIds}/{backPolicyId}", "/configBackPolicy/{agentIds}"})
    public ResultVO<Boolean> configBackPolicy(@PathVariable("agentIds") List<Long> agentIds, @PathVariable(value = "backPolicyId", required = false) Long backPolicyId) {
        return ResultVO.ok(agentService.configBackPolicy(agentIds, backPolicyId));
    }

}
