package com.xxl.job.core.biz.client;

import com.xxl.job.core.biz.AdminBiz;
import com.xxl.job.core.biz.model.HandleCallbackParam;
import com.xxl.job.core.biz.model.JobInfo;
import com.xxl.job.core.biz.model.RegistryParam;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.util.XxlJobRemotingUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * admin api test
 *
 * @author xuxueli 2017-07-28 22:14:52
 */
@Service
public class AdminBizClient implements AdminBiz {

    public AdminBizClient() {
    }
    public AdminBizClient(String addressUrl, String accessToken) {
        this.addressUrl = addressUrl;
        this.accessToken = accessToken;

        // valid
        if (!this.addressUrl.endsWith("/")) {
            this.addressUrl = this.addressUrl + "/";
        }
    }

    private String addressUrl ;
    private String accessToken;
    private int timeout = 3;


    @Override
    public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList) {
        return XxlJobRemotingUtil.postBody(addressUrl+"api/callback", accessToken, timeout, callbackParamList, String.class);
    }

    @Override
    public ReturnT<String> registry(RegistryParam registryParam) {
        return XxlJobRemotingUtil.postBody(addressUrl + "api/registry", accessToken, timeout, registryParam, String.class);
    }

    @Override
    public ReturnT<String> registryRemove(RegistryParam registryParam) {
        return XxlJobRemotingUtil.postBody(addressUrl + "api/registryRemove", accessToken, timeout, registryParam, String.class);
    }


    @Override
    public ReturnT<String> jobAdd(JobInfo jobInfo) {
        return XxlJobRemotingUtil.postBody(addressUrl + "api/jobAdd", accessToken, timeout, jobInfo, String.class);
    }
    @Override
    public ReturnT<String> jobRemove(Integer jobId) {
        return XxlJobRemotingUtil.postBody(addressUrl + "api/jobRemove", accessToken, timeout, jobId, String.class);
    }
    @Override
    public ReturnT<String> jobStop(Integer jobId) {
        return XxlJobRemotingUtil.postBody(addressUrl + "api/jobStop", accessToken, timeout, jobId, String.class);
    }
    @Override
    public ReturnT<String> jobStart(Integer jobId) {
        return XxlJobRemotingUtil.postBody(addressUrl + "api/jobStart", accessToken, timeout, jobId, String.class);
    }
    @Override
    public ReturnT<String> jobUpdate(JobInfo jobInfo) {
        return XxlJobRemotingUtil.postBody(addressUrl + "api/jobUpdate", accessToken, timeout, jobInfo, String.class);
    }

    @Override
    public ReturnT<String> jobTrigger(Integer jobId) {
        return XxlJobRemotingUtil.postBody(addressUrl + "api/jobTrigger", accessToken, timeout, jobId, String.class);
    }

    @Override
    public ReturnT<String> getJobById(Integer jobId) {
        return XxlJobRemotingUtil.postBody(addressUrl + "api/getJobById", accessToken, timeout, jobId, String.class);
    }

//    @Override
//    public ReturnT<String> stopByIds(List<Integer> jobIds) {
//        return XxlJobRemotingUtil.postBody(addressUrl + "api/jobStopByIds", accessToken, timeout, jobIds, String.class);
//    }

}
