package com.xxl.job.executor.common.entity;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: oauth授权码
 * @Author: jeecg-boot
 * @Date: 2019-07-09
 * @Version: V1.0
 */
@Data
@TableName("ctop_oauth_token")
public class CtopOauthToken implements Serializable {

    /**
     * 主键ID
     */
    @TableId
    private String id;
    /**
     * 媒体ID（外部广告平台类型，快手/头条/抖音等）
     */
    private String mediaId;
    /**
     * 广告主ID(自己平台用户内部id)
     */
    private String advertiserId;
    /**
     * 广告账户ID（相当于头条返回的access_token数据中的advertiser_id）
     */
    private Long accountId;
    /**
     * 访问令牌
     */
    private String accessToken;
    /**
     * 访问令牌有效期
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date accessTokenExpiresIn;
    /**
     * 刷新令牌
     */
    private String refreshToken;


    private Integer agentType;

    /**
     * 刷新令牌有效期
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date refreshTokenExpiresIn;


    /**
     * 创建时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    /**
     * 修改时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    public CtopOauthToken(String userId, JSONObject data) {
        Long accountId = data.getLong("advertiser_id");
        this.id = String.valueOf(accountId);
        this.advertiserId = userId;
        this.mediaId = "1";
        this.accountId = accountId;
        String accessToken = data.getString("access_token");
        Integer refreshTokenExpiresIn = data.getInteger("refresh_token_expires_in");
        Integer expiresIn = data.getInteger("expires_in");
        String refreshToken = data.getString("refresh_token");
        this.accessToken = accessToken;
        this.refreshToken = refreshToken;
        Date now = new Date();
        this.accessTokenExpiresIn = addSecond(now, expiresIn);
        this.refreshTokenExpiresIn = addSecond(now, refreshTokenExpiresIn);
        this.createTime = now;
        this.updateTime = now;
    }

    public CtopOauthToken() {
    }

    public CtopOauthToken(CtopOauthToken token, JSONObject data) {
        this.advertiserId = token.getAdvertiserId();
        this.mediaId = "1";
        this.accountId = token.getAccountId();
        String accessToken = data.getString("access_token");
        Integer refreshTokenExpiresIn = data.getInteger("refresh_token_expires_in");
        Integer expiresIn = data.getInteger("expires_in");
        String refreshToken = data.getString("refresh_token");
        this.accessToken = accessToken;
        this.refreshToken = refreshToken;
        Date now = new Date();
        this.accessTokenExpiresIn = addSecond(now, expiresIn);
        this.refreshTokenExpiresIn = addSecond(now, refreshTokenExpiresIn);
        this.updateTime = now;
    }

    private static Date addSecond(Date date, int seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, seconds);
        return calendar.getTime();
    }

    @Override
    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return id +
                "," + mediaId +
                "," + advertiserId +
                "," + accountId +
                "," + accessToken +
                "," + sdf.format(accessTokenExpiresIn) +
                "," + refreshToken +
                "," + sdf.format(refreshTokenExpiresIn) +
                "," + sdf.format(createTime) +
                "," + sdf.format(updateTime);
    }
}
