import r from '@/utils/request'
import { getRequestId } from '@/utils/misc'
export interface StandardResponse {
    code: number
    message: string
    meta: any
    records: any
}

const URL_GET_JOB = '/api/agent/job'
const URL_GET_QUEUE = '/api/agent/queue'
const URL_GET_FILE = '/api/file/browse'
const URL_GET_USER = '/api/user/session'
const URL_PUT_USER = '/api/user/signin'
const SUCCESS = 200
const FAILED = 500
const PROCESSING = 202
interface RequestActionParams { // 定义Job接口
    name: string
    value: any
}
export interface RequestBody { // 定义Job接口
    id: string
    action: string
    params: Array<RequestActionParams>
}
export interface JobData {
    id: number
    job_category: string
    job_name: string
    job_details: string
    job_creator: string
    job_status: string
    status: number
    executor: string
    created: Date
    updated: Date
}
export const JobSatus = [
    //primary success info warning danger
    { "label": "无效", "value": -1, "tag": "danger", "could_be_set": false },
    { "label": "准备中", "value": 0, "tag": "primary", "could_be_set": true },
    { "label": "运行中", "value": 1, "tag": "success", "could_be_set": false },
    { "label": "完成", "value": 2, "tag": "success", "could_be_set": false },
    { "label": "失败", "value": 3, "tag": "warning", "could_be_set": false },
    { "label": "取消", "value": 4, "tag": "info", "could_be_set": true },
    { "label": "等待", "value": 5, "tag": "info", "could_be_set": false },
    { "label": "暂停", "value": 6, "tag": "info", "could_be_set": true },
    { "label": "恢复", "value": 7, "tag": "info", "could_be_set": false },
    { "label": "重启", "value": 8, "tag": "info", "could_be_set": false },
    { "label": "跳过", "value": 9, "tag": "info", "could_be_set": true },
    { "label": "重试", "value": 10, "tag": "warning", "could_be_set": true },
]

export function getJobStatus(status: number): string {
    var result = JobSatus.find(item => item.value == status)
    if (result) {
        return result.label
    } else {
        return '未知'
    }
}

export function getJobStatusTag(status: number): string {
    var result = JobSatus.find(item => item.value == status)
    if (result) {
        return result.tag
    } else {
        return 'primary'
    }
}
export function serverRequest(url: string, body: RequestBody): Promise<StandardResponse> {
    return new Promise((resolve, reject) => {
        r.request<StandardResponse>({ url: url, method: 'POST', data: body }).then(result => {
            var response_data: StandardResponse = result as StandardResponse
            if (response_data.code == SUCCESS) {
                resolve(response_data)
            } else {
                reject(response_data)
            }
        })
    })
}
export function serverGetRequest(url: string,): Promise<StandardResponse> {
    return new Promise((resolve, reject) => {
        r.request<StandardResponse>({ url: url, method: 'GET' }).then(result => {
            var response_data: StandardResponse = result as StandardResponse
            if (response_data.code == SUCCESS) {
                resolve(response_data)
            } else {
                reject(response_data)
            }
        })
    })
}

export function getJob(job_id: number): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_job',
        params: [
            { name: 'job_id', value: job_id },
        ]
    }
    return serverRequest(URL_GET_JOB, data)
}
export function getJobs(categroy: string, name: string, page: number = 1, page_size: number = 10): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_jobs',
        params: [
            { name: 'queue_category', value: categroy },
            { name: 'queue_name', value: name },
            { name: 'page', value: page },
            { name: 'page_size', value: page_size },
        ]
    }
    return serverRequest(URL_GET_QUEUE, data)
}
export function getQueue(categroy: string, name: string): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_queue',
        params: [
            { name: 'queue_category', value: categroy },
            { name: 'queue_name', value: name },
        ]
    }
    return serverRequest(URL_GET_QUEUE, data)
}

export function createJob(params: any): Promise<StandardResponse> {
    // console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'create_job',
        params: [
            { name: 'job_category', value: params['job_category'] },
            { name: 'job_name', value: params['job_name'] },
            { name: 'job_details', value: params['job_details'] },
            { name: 'job_creator', value: params['job_creator'] },
            { name: 'queue_category', value: params['queue_category'] },
            { name: 'queue_name', value: params['queue_name'] },
        ]
    }
    return serverRequest(URL_GET_JOB, data)
}
export function deleteJob(params: any): Promise<StandardResponse> {
    // console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'delete_job',
        params: [
            { name: 'job_id', value: params['job_id'] },
        ]
    }
    return serverRequest(URL_GET_JOB, data)
}
export function updateJobStatus(job: JobData): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'update_job_status',
        params: [
            { name: 'job_id', value: job.id },
            { name: 'status', value: job.status },
        ]
    }
    return serverRequest(URL_GET_JOB, data)
}
export function updateJob(job: JobData): Promise<StandardResponse> {

    var data: RequestBody = {
        id: getRequestId(),
        action: 'update_job',
        params: [
            { name: 'job_id', value: job.id },
            { name: 'job_category', value: job.job_category },
            { name: 'job_name', value: job.job_name },
            { name: 'job_details', value: job.job_details },
            { name: 'job_creator', value: job.job_creator },
            { name: 'status', value: job.status },

        ]
    }
    return serverRequest(URL_GET_JOB, data)
}
export function putQueueJob(params: any): Promise<StandardResponse> {
    // console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'put_job',
        params: [
            { name: 'queue_category', value: params['queue_category'] },
            { name: 'queue_name', value: params['queue_name'] },
            { name: 'job_id', value: params['job_id'] },
        ]
    }
    return serverRequest(URL_GET_QUEUE, data)
}

export function browseJobFile(params: any): Promise<StandardResponse> {
    // console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'browse_job_file',
        params: [
            { name: 'job_id', value: params['job_id'] },
            { name: 'path', value: params['path'] },
        ]
    }
    return serverRequest(URL_GET_FILE, data)
}

export function userSignin(params: any): Promise<StandardResponse> {
    // console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'signin',
        params: [
            { name: 'username', value: params['username'] },
            { name: 'password', value: params['password'] },
            { name: 'email', value: params['email'] },
            { name: 'full_name', value: params['full_name'] },
        ]
    }
    return serverRequest(URL_PUT_USER, data)
}
export function userLogin(params: any): Promise<StandardResponse> {
    // console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'login',
        params: [
            { name: 'username', value: params['username'] },
            { name: 'password', value: params['password'] },
        ]
    }
    return serverRequest(URL_GET_USER, data)
}

export function userLogout(params: any): Promise<StandardResponse> {
    // console.log('logout')
    var data: RequestBody = {
        id: getRequestId(),
        action: 'logout',
        params: [{ name: 'session_id', value: params['username'] }]
    }
    return serverRequest(URL_GET_USER, data)
}

export function getQueueSummary(params: any): Promise<StandardResponse> {
    // console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_queue_summary',
        params: [

        ]
    }
    return serverRequest(URL_GET_QUEUE, data)
}

export function getFileContent(url: string): Promise<StandardResponse> {
    return serverGetRequest(url)
}

export function getKnowledgeBase(params: any = { name: "", pageNo: 1, pageSize: 10 }): Promise<StandardResponse> {
    return serverGetRequest(`/open-platform/knowledge-base/?name=${params.name}&pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
}

export function getKnowledgeBaseFilesList(params: any): Promise<StandardResponse> {
    return serverGetRequest(`/open-platform/knowledge-base/${params.kbId}/files/?file_name=${params.file_name}&pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
}