package com.fs.app.controller;

import cn.hutool.core.util.ObjectUtil;
import com.fs.newAdv.integration.client.IAccessTokenClient;
import com.fs.newAdv.integration.client.IApiClient;
import com.fs.newAdv.integration.factory.AdvertiserHandlerFactory;
import com.fs.common.result.Result;
import com.fs.newAdv.domain.PromotionAccount;
import com.fs.newAdv.enums.AdvertiserTypeEnum;
import com.fs.newAdv.service.IPromotionAccountService;
import com.fs.newAdv.vo.AccessTokenByAuthCodeVo;
import com.fs.newAdv.vo.AccessTokenVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 广告商回调接口
 */
@Slf4j
@RestController
@RequestMapping("/callBack")
public class CallbackController {

    @Autowired
    private AdvertiserHandlerFactory advertiserHandlerFactory;
    @Autowired
    private IPromotionAccountService promotionAccountService;

    /**
     * 百度回调接口
     *
     * @param request
     * @return
     */
    @GetMapping("/baidu/getAuthCode")
    public Result<String> getBaiduAuthCode(HttpServletRequest request) {
        getAccessToken(AdvertiserTypeEnum.BAIDU, request.getParameter("state"), request.getParameter("authCode"));
        return Result.success("");
    }

    /**
     * 巨量回调接口
     *
     * @param request
     * @return
     */
    @GetMapping("/oceanEngine/getAuthCode")
    public Result<String> getOceanEngineAuthCode(HttpServletRequest request) {
        getAccessToken(AdvertiserTypeEnum.OCEANENGINE, request.getParameter("state"), request.getParameter("auth_code"));
        return Result.success("");
    }

    /**
     * 腾讯回调接口
     *
     * @param request
     * @return
     */
    @GetMapping("/tencent/getAuthCode")
    public Result<String> getTencentTAuthCode(HttpServletRequest request) {
        getAccessToken(AdvertiserTypeEnum.TENCENT, request.getParameter("state"), request.getParameter("authorization_code"));
        return Result.success("");
    }

    /**
     * OPPO回调接口
     *
     * @param request
     * @return
     */
    @GetMapping("/oppo/getAuthCode")
    public Result<String> getOppoAuthCode(HttpServletRequest request) {
        getAccessToken(AdvertiserTypeEnum.OCEANENGINE, request.getParameter("state"), request.getParameter("auth_code"));
        return Result.success("");
    }

    /**
     * vivo回调接口
     *
     * @param request
     * @return
     */
    @GetMapping("/vivo/getAuthCode")
    public Result<String> getViVoAuthCode(HttpServletRequest request) {
        getAccessToken(AdvertiserTypeEnum.OCEANENGINE, request.getParameter("state"), request.getParameter("auth_code"));
        return Result.success("");
    }

    /**
     * iqiyi回调接口
     *
     * @param request
     * @return
     */
    @GetMapping("/iqiyi/getAuthCode")
    public Result<String> getiqiyiAuthCode(HttpServletRequest request) {
        getAccessToken(AdvertiserTypeEnum.OCEANENGINE, request.getParameter("state"), request.getParameter("auth_code"));
        return Result.success("");
    }

    private void getAccessToken(AdvertiserTypeEnum advertiserType, String state, String authCode) {
        PromotionAccount byId = promotionAccountService.getById(state);
        // 获取请求参数
        IApiClient apiClient = advertiserHandlerFactory.getApiClient(advertiserType);
        IAccessTokenClient tokenClient = (IAccessTokenClient) apiClient;
        AccessTokenVo accessToken = tokenClient.getAccessTokenByAuthCode(AccessTokenByAuthCodeVo
                .builder()
                .adAccountId(byId.getAdAccountId())
                .appId(byId.getAppId())
                .authCode(authCode)
                .appSecret(byId.getAppSecret())
                .appSecret(byId.getAppSecret())
                .build());
        if (ObjectUtil.isNotEmpty(accessToken)) {
            byId.setAccessToken(accessToken.getAccessToken());
            byId.setRefreshToken(accessToken.getRefreshToken());
            byId.setExpireTime(accessToken.getExpireTime());
            byId.setUpdateTime(LocalDateTime.now());
            promotionAccountService.updateById(byId);
        } else {
            log.error("获取accessToken失败:{}", byId.getId());
        }
    }
}
