/** @format */
import * as React from 'react';
import {
    ONTIMEQUOTA,
    GET_MERCHANT_DATA_API,
    DELETE_DATA_API,
    GET_DATA_API_DESC,
    PUT_MERCHANT_DATA_API_LIMIT,
    PUT_UPDATE_PRODUCT_ALIAS,
} from '../const/path';
import Axios from '../utils/xhr';
const page_size = 10;
export interface ModifiableProps {
    day_info: {
        count_thresholds: string; //日调用量阈值
        count_thresholds_enable: boolean; //是否开启阈值
        day_combo_count: number; //日调用量上限 /当前量
        day_combo_days: number;
        day_combo_id: number;
        day_thresholds: string; //剩余有效天数阈值
        day_total_count: number; //日即查量上限
        ip_whitelist: string;
        ip_whitelist_enable: boolean;
        remain_day_total_count: number;
        effective_day: number; //当前剩余天数
    };
    total_info: {
        count_thresholds: string; //总调用量阈值
        count_thresholds_enable: boolean; //是否开启阈值
        day_total_count: number; //日即查量上限
        ip_whitelist: string;
        ip_whitelist_enable: boolean;
        remain_day_total_count: number;
        total_combo_count: number; //总调用量上限 /当前量
        total_combo_id: number;
        total_combo_day_count: number;
    };
}

export interface ReciveOnTimeQuotoRecordProps {
    data_api_name: string;
    day_count: number;
    day_remain_count: number;
    day_used_count: number;
    effective_day: number;
    is_free: boolean;
    merchant_data_api_id: number;
    query_type_name: string;
    status_code: number;
    status: string;
    total_day: number;
    use_time: string;
    modifiable: ModifiableProps;
    day_combo_count: number;
    total_combo_count: string;
    day_total_count: string;
    remain_day_total_count: string;
    count_thresholds: string;
    day_thresholds: string;
    alias: string;
}
export interface DataApiByTimeRecordProps extends ReciveOnTimeQuotoRecordProps {
    index: number;
    id: number;
}
interface BaseParamsProps {
    merchant_id: number | string;
    page_number?: number;
    is_all?: boolean;
    page_size?: number;
    data_api_name?: string;
}
/**
 * 获取商户下的数据API 按时
 */
export const userMerchantDataApiByTime = (params: BaseParamsProps) => {
    const [state, setState] = React.useState({
        dataSource: [],
        loading: false,
        error: null,
        total: 0,
        deepData: {
            page_size,
            data_api_name: '',
            page_number: 1,
            is_all: false,
            ...params,
        },
    });
    const requestData = (params?: Partial<BaseParamsProps>) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({...preState, loading: true, error: null, deepData: combineParams}));
        Axios({
            url: ONTIMEQUOTA,
            params: combineParams,
        })
            .then(({code, data, msg}) => {
                if (code === 0) {
                    if (!data.merchant_data_api_day_item) {
                        data.merchant_data_api_day_item = [];
                    }
                    setState(preState => {
                        const basePage = (data.page_number - 1) * data.page_size + 1;
                        return {
                            ...preState,
                            loading: false,
                            total: data.total,
                            dataSource: data.merchant_data_api_day_item.map(
                                (element: ReciveOnTimeQuotoRecordProps, index: number) => {
                                    return {
                                        ...element,
                                        id: element.merchant_data_api_id,
                                        index: basePage + index,
                                    };
                                },
                            ),
                        };
                    });
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
            });
    };
    React.useEffect(() => {
        requestData(params);
    }, []);
    return [state, requestData] as const;
};

interface ReciveQuotaQuotaRecordProps {
    data_api_name: string;
    day_count: number;
    is_free: boolean;
    merchant_data_api_id: number;
    modifiable: ModifiableProps;
    query_type_name: string;
    remain_count: number;
    status: string;
    status_code: number;
    total_count: number;
    use_time: string;
    used_count: number;
    alias: string;
}
export interface DataApiByQuotoRecordProps extends ReciveQuotaQuotaRecordProps {
    id: number;
    index: number;
}
/**
 * 商户下的数据API 按次
 */
export const useMerchantDataApiByQuoto = (params: BaseParamsProps) => {
    const [state, setState] = React.useState({
        dataSource: [],
        loading: false,
        error: null,
        total: 0,
        deepData: {
            page_size,
            data_api_name: '',
            page_number: 1,
            is_all: false,
            merchant_id: 0,
            ...params,
        },
    });
    const requestData = (params?: Partial<BaseParamsProps>) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({...preState, loading: true, error: null, deepData: combineParams}));
        Axios({
            url: GET_MERCHANT_DATA_API,
            params: combineParams,
        })
            .then(({code, data, msg}) => {
                if (code === 0) {
                    if (!data.merchant_data_api_total_item) {
                        data.merchant_data_api_total_item = [];
                    }
                    setState(preState => {
                        const basePage = (data.page_number - 1) * data.page_size + 1;
                        return {
                            ...preState,
                            loading: false,
                            total: data.total,
                            dataSource: data.merchant_data_api_total_item.map(
                                (element: ReciveQuotaQuotaRecordProps, index: number) => {
                                    return {
                                        ...element,
                                        id: element.merchant_data_api_id,
                                        index: basePage + index,
                                    };
                                },
                            ),
                        };
                    });
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
            });
    };
    React.useEffect(() => {
        requestData(params);
    }, []);
    return [state, requestData] as const;
};
/**
 * 商户新增数据Api
 */
export const useMerchantInsertDataApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (data: {combo_id: number; merchant_id: number; query_type_id: number}) => {
        return Axios({
            url: '/merchant_api/merchant_data_api',
            method: 'POST',
            data: data,
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false}));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                    return msg;
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
                return error.message;
            });
    };
    return [state, requestData] as const;
};
/**
 *  删除数据API
 */
export const useDeleteDataApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (id: number) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: DELETE_DATA_API + '/' + id,
            method: 'DELETE',
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false, error: null}));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                    return msg;
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
                return error.message;
            });
    };
    return [state, requestData] as const;
};
/**
 * 获取数据API的文档路径
 */

export const useDataQueryDesc = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: {merchant_data_api_id: number; file_type: 0 | 1}) => {
        return Axios({
            url: GET_DATA_API_DESC,
            params: params,
        })
            .then(({code, msg, download_file}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false}));
                    return [null, download_file];
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                    return [msg, null];
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
                return [error.message, null];
            });
    };
    return [state, requestData] as const;
};

// {
//   "count_thresholds": "string",// 次 总调用量阈值 时 日调用量阈值
//   "count_thresholds_enable": true,
//   "day_combo_id": 0, // 时 套餐ID
//   "day_combo_increase_count": 0, //时 日调用上限增加值
//   "day_combo_increase_days": 0, //时 剩余有效天数增加值
//   "day_thresholds": "string",  // 时 剩余有效天数阈值
//   "day_total_count": 0,  时/次   //即查
//   "ip_whitelist": "string", 时/次  //白名单
//   "ip_whitelist_enable": true, 时/次  //开启白名单
//   "merchant_data_api_id": 0, 时/次  //数据API Id
//   "total_combo_id": 0, // 次 套餐ID
//   "total_combo_increase_count": 0 //次 总调用量增加值
// }
export interface TotalLimitParams {
    total_combo_id: number;
    count_thresholds_enable: boolean;
    count_thresholds: string;
    day_total_count: number;
    total_combo_increase_count: number;
    ip_whitelist_enable: boolean;
    ip_whitelist: string;
    total_combo_day_count: number;
}
export interface DayLimitParams {
    count_thresholds: string;
    count_thresholds_enable: boolean;
    day_combo_increase_count: number;
    day_combo_increase_days: number;
    day_thresholds: string;
    day_total_count: number;
    ip_whitelist: string;
    ip_whitelist_enable: boolean;
    day_combo_id: number;
}
/**
 * 修改商户下面的数据API(限额)
 */
export const useUpdateMerchantDataApiByLimit = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (
        merchant_id: number,
        merchant_data_api_id: number,
        data: TotalLimitParams | DayLimitParams,
    ) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_MERCHANT_DATA_API_LIMIT + '/' + merchant_id,
            method: 'PUT',
            data: {...data, merchant_data_api_id},
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false, error: null}));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                    return msg;
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
                return error.message;
            });
    };
    return [state, requestData] as const;
};

/**
 * 修改商品别名
 */

export const useUpdateProductAlias = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const updateAlias = (alias: string, merchant_data_api_id: number) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_UPDATE_PRODUCT_ALIAS,
            method: 'PUT',
            data: {alias, merchant_data_api_id},
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false, error: null}));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                    return msg;
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
                return error.message;
            });
    };
    return [state, updateAlias] as const;
};
