/*
 * @Author: Kane
 * @Date: 2023-09-11 09:59:00
 * @LastEditors: Kane
 * @FilePath: /task_schedule/src/utils/reward.ts
 * @Description: 奖励相关的API
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
import { type AxiosResponse } from "axios";
import { service as instance } from "./api/request.ts";
import { API_URL } from "./api/config.ts";

interface RewardProject
{
    rewardCode: number;
    rewardName: string;
}

interface RewardProjectResponse
{
    success: boolean;
    message: string;
    rewardList: RewardProject[] | null;
}

interface RewardGainer
{
    recID: number;
    callerName: string;
    callerCode: string;
    rewardProjectCode: string;
    rewardProjectName: string;
}

interface RewardGainerResponse
{
    success: boolean;
    message: string;
    gainerList: RewardGainer[] | null;
}

interface AddRewardTelsalerRequest
{
    telsalerName: string;
    rewardIndex: number;
}

interface AddRewardTelsalerResponse
{
    success: boolean;
    message: string;
}

interface UpdateRewardTelsalerRequest
{
    recordID: number,
    telsalerName: string;
    rewardIndex: number;
}

interface UpdateRewardTelsalerResponse
{
    success: boolean;
    message: string;
}

interface DeleteRewardTelsalerRequest
{
    recordID: number[];
}

interface DeleteRewardTelsalerResponse
{
    success: boolean;
    message: string;
}

/**
 * 请求奖项清单
 * @param handler 用于处理请求数据的回调函数；
 */
function requestRewardPorjectsList( handler: any ): void
{
    if ( handler === undefined || handler === null )
    {
        return;
    }

    const rewardResponse: RewardProjectResponse = {
        success: false,
        message: "",
        rewardList: [],
    };

    instance.request({
        url: API_URL.URL_RWARD_PROJECTS,
        method: "post",
    })
        // 请求成功，检查服务器返回结果
        .then(( response: AxiosResponse<any, any> ): void =>
        {
            const data = response.data ?? {};
            rewardResponse.success = data.success ?? false;
            rewardResponse.message = data.message ?? "";
            rewardResponse.rewardList = checkRewardProjects( data.rewardList ?? []); // eslint-disable-line

            handler( rewardResponse, null );
        })
        .catch(( error: any ): void =>
        {
            console.log( error );

            rewardResponse.success = false;

            handler( rewardResponse, error );
        });
}

/**
 * 请求获奖人员清单
 * @param handler 处理请求结果的回调函数
 */
function requestRewardGainers( handler: any ): void
{
    if ( handler === undefined || handler === null )
    {
        return;
    }

    const rewardResponse: RewardGainerResponse = {
        success: false,
        message: "",
        gainerList: [],
    };

    instance.request({
        url: API_URL.URL_REWARD_GAINERS,
        method: "post",
    })
        .then(( response: AxiosResponse<any, any> ): void =>
        {
            const data = response.data ?? {};

            rewardResponse.success = data.success;
            rewardResponse.message = data.message;
            rewardResponse.gainerList = checkRewardGainers( data.gainerList ); // eslint-disable-line

            handler( rewardResponse, null );
        })
        .catch(( error: any ): void =>
        {
            console.log( error );

            rewardResponse.success = false;

            handler( rewardResponse, error );
        });
}

function addRewardTelsaler( data: AddRewardTelsalerRequest, handler: any ): void
{
    if ( handler === undefined || handler === null )
    {
        return;
    }

    const addResponse: AddRewardTelsalerResponse = {
        success: false,
        message: "",
    };

    console.log( "URL：", API_URL.URL_ADD_REWARD_TELSALER );

    instance.request({
        method: "post",
        url: API_URL.URL_ADD_REWARD_TELSALER,
        data,
    })
        .then(( response: AxiosResponse<any, any> ): void =>
        {
            const data: AddRewardTelsalerResponse = response.data ?? {};

            addResponse.success = data.success ?? false;
            addResponse.message = data.message ?? "";

            handler( addResponse );
        })
        .catch(( error ): void =>
        {
            console.log( error );

            addResponse.success = false;
            addResponse.message = "请求添加获奖坐席失败，请查看控制台！";

            handler( addResponse, error );
        });
}

/**
 * 请求更新获奖坐席，根据请求结果调用handler函数。
 * @param data 请求数据
 * @param handler 处理请求结果数据的回调函数。
 */
function updateTelsalerReward( data: UpdateRewardTelsalerRequest, handler: any ): void
{
    if ( handler === undefined || handler === null )
    {
        return;
    }

    const updateResponse: UpdateRewardTelsalerResponse = {
        success: false,
        message: "",
    };

    instance.request({
        method: "post",
        url: API_URL.URL_UPDATE_REWARD_TELSALER,
        data,
    })
        .then(( response: AxiosResponse<any, any> ): any =>
        {
            const data: UpdateRewardTelsalerResponse = response.data ?? {};

            updateResponse.success = data.success ?? false;
            updateResponse.message = data.message ?? "";

            handler( updateResponse );
        })
        .catch(( error: any ): void =>
        {
            updateResponse.success = false;
            updateResponse.message = "请求更新获奖坐席失败，请查看控制台！";

            console.log( error );
            handler( updateResponse, error );
        });
}

/**
 * 请求删除获奖坐席。
 * @param data 请求数据
 * @param handler 处理请求结果的回调函数
 */
function deleteRewardTelsaler( data: DeleteRewardTelsalerRequest, handler: any ): void
{
    if ( handler === undefined || handler === null )
    {
        return;
    }

    const deleteResponse: DeleteRewardTelsalerResponse = {
        success: false,
        message: "",
    };

    instance.request({
        method: "post",
        url: API_URL.URL_DEL_REWARD_TELSALER,
        data,
    })
        .then(( response: AxiosResponse<any, any> ): void =>
        {
            const data: DeleteRewardTelsalerResponse = response.data ?? {};

            deleteResponse.success = data.success ?? false;
            deleteResponse.message = data.message ?? "";

            handler( deleteResponse );
        })
        .catch(( error: any ): void =>
        {
            deleteResponse.message = "请求删除获奖坐席失败，请检查日志！";
            deleteResponse.success = false;

            console.log( error );

            handler( deleteResponse, error );
        });
}

// **功能函数 **********************************/
function checkRewardProjects( data: any[]): RewardProject[] | null
{
    const rewardList: RewardProject[] = [];

    // 检查一下参数的类型，如果不是数组返回 null
    if ( !( data instanceof Array ))
    {
        return null;
    }

    data.forEach(( item: any ) =>
    {
        const reward = {
            rewardCode: item.rewardCode ?? "",
            rewardName: item.rewardName ?? "",
        };

        rewardList.push( reward );
    });

    return rewardList;
}

function checkRewardGainers( gainers: any[]): RewardGainer[] | null
{
    const gainerList: RewardGainer[] = [];

    if ( !( gainers instanceof Array ))
    {
        return null;
    }

    gainers.forEach(( item: RewardGainer ): void =>
    {
        const gainer: RewardGainer = {
            recID: item.recID ?? "",
            callerName: item.callerName ?? "",
            callerCode: item.callerCode ?? "",
            rewardProjectCode: item.rewardProjectCode ?? "",
            rewardProjectName: item.rewardProjectName ?? "",
        };

        gainerList.push( gainer );
    });

    return gainerList;
}

export {
    type RewardProject,
    type RewardGainer,
    type RewardProjectResponse,
    type RewardGainerResponse,
    type DeleteRewardTelsalerRequest,
    type DeleteRewardTelsalerResponse,
    type AddRewardTelsalerRequest,
    type AddRewardTelsalerResponse,
    type UpdateRewardTelsalerRequest,
    type UpdateRewardTelsalerResponse,
    requestRewardPorjectsList,
    requestRewardGainers,
    addRewardTelsaler,
    updateTelsalerReward,
    deleteRewardTelsaler
};
