/** @format */

import * as React from 'react';
import {
    GET_SOURCE_API_LIST,
    PUT_SOURCE_API,
    GET_SOURCE_MERCHANT,
    POST_SOURCE_MERCNAHNT,
    PUT_SOURCE_MERCNAHNT,
    DELELE_SOURCE_API,
    GET_API_FUSING_INFO,
    PUT_API_FUSING_INFO,
} from '../const/path';
import Axios from '../utils/xhr';
const page_size = 10;
interface SourceApiParams {
    page_number?: number;
    provider_id: number;
    page_size?: number;
}

interface ReciveSourceApiRecordProps {
    threshold_timeout: number;
    threshold_fail_rate: number;
    threshold_norecord_rate: number;
    warning_enable: boolean;
    thirdpart_api_name: string;
    provider_api_code: string;
    thirdpart_host: string;
    thirdpart_api_router: string;
    thirdpart_api_method: string;
    count_type: number;
    count_key: string;
    count_value: string;
    cost: String;
    count: number;
    state: number;
    create_time: string;
    provider_api_id: number;
    timeout: number;
    filters: any[];
}
export interface SourceApiRecordProps extends ReciveSourceApiRecordProps {
    index: number;
    id: number;
}

interface SourceApiState {
    loading: boolean;
    error: null | string;
    total: number;
    dataSource: SourceApiRecordProps[];
    deepData: SourceApiParams;
}

/**
 * 获取数据源下面的API列表
 */
export const useSourceApiList = (params: SourceApiParams) => {
    const [state, setState] = React.useState<SourceApiState>({
        loading: false,
        error: null,
        total: 0,
        dataSource: [],
        deepData: {
            page_number: 1,
            ...params,
        },
    });
    const requestData = (params?: Partial<SourceApiParams>) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({
            ...preState,
            loading: true,
            error: null,
            deepData: combineParams,
        }));
        Axios({
            url: GET_SOURCE_API_LIST,
            params: combineParams,
        })
            .then(({code, msg, data}) => {
                if (code === 0) {
                    if (!data.provider_api_list) {
                        data.provider_api_list = [];
                    }
                    const basePage = (data.page_number - 1) * data.page_size + 1;
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        error: null,
                        total: data.total,
                        dataSource: data.provider_api_list.map((element: ReciveSourceApiRecordProps, index: number) => {
                            element.filters = element.filters || [];
                            return {
                                ...element,
                                index: basePage + index,
                                id: element.provider_api_id,
                                filters: element.filters.map((item, index) => ({...item, id: 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 AlertSetParams {
    threshold_timeout: number;
    threshold_fail_rate: number;
    threshold_norecord_rate: number;
    warning_enable: boolean;
    id: number;
}
//修改数据源接口预警阈值
export const usePutSourceApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: AlertSetParams) => {
        setState(preState => ({...preState, loading: true, error: null}));
        const {id, ...rest} = params;
        return Axios({
            url: PUT_SOURCE_API + '/' + id,
            method: 'PUT',
            data: rest,
        })
            .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;
};
/**
 * 获取数据源下面的商户信息
 *
 */
interface MerchantInfo {
    api_name: string;
    day_count: number;
    enable: boolean;
    inventory: number;
    merchant_child_api_id: number;
    merchant_name: string;
}
interface MerchantRecordProps extends MerchantInfo {
    index: number;
    id: number;
}
interface MerchantState {
    loading: boolean;
    error: null;
    dataSource: MerchantRecordProps[];
}
/**
 * 获取 数据源 API下面的商户
 */
export const useProvideApiMerchant = () => {
    const [state, setState] = React.useState<MerchantState>({
        loading: false,
        error: null,
        dataSource: [],
    });
    const requestData = (id: number) => {
        setState(preState => ({...preState, loading: true, error: null}));
        Axios({
            url: GET_SOURCE_MERCHANT,
            params: {
                provider_api_id: id,
            },
        })
            .then(({code, provider_merchant_api_list, msg}) => {
                provider_merchant_api_list = provider_merchant_api_list || [];
                if (code === 0) {
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        dataSource: provider_merchant_api_list.map((element: MerchantInfo, index: number) => {
                            return {
                                ...element,
                                index: index + 1,
                                id: element.merchant_child_api_id,
                            };
                        }),
                    }));
                } else {
                    setState(preState => ({...preState, loading: true, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: true, error: error.message}));
            });
    };
    return [state, requestData] as const;
};

interface CreateApiParamsProps {
    cost: number;
    count: number;
    count_key: string;
    count_type: 0 | 1;
    count_value: string;
    provider_api_code: string;
    state: 0 | 1;
    thirdpart_api_method: string;
    thirdpart_api_name: string;
    thirdpart_api_router: string;
    thirdpart_host: string;
    filters: any[];
}

/**
 * 数据源新增API
 */
export const useInsertApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: CreateApiParamsProps) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: POST_SOURCE_MERCNAHNT,
            method: 'POST',
            data: params,
        })
            .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 usePutApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (data: any) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_SOURCE_MERCNAHNT,
            method: 'PUT',
            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;
};
export const useDeleteSourceApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (id: number) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: DELELE_SOURCE_API,
            method: 'DELETE',
            params: {
                provider_api_id: id,
            },
        })
            .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 useFusingApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
        dataSource: {},
    });
    const requestData = (code: number) => {
        setState(preState => ({...preState, loading: true, error: null}));
        Axios({
            url: GET_API_FUSING_INFO,
            params: {
                provider_api_code: code,
            },
        })
            .then(({code, data, msg}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false, dataSource: data}));
                } else {
                    setState(preState => ({...preState, loading: true, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: true, error: error.message}));
            });
    };
    return [state, requestData] as const;
};

/**
 * 更改API的熔断信息
 */
export const useUpdateFusingApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (data: any) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_API_FUSING_INFO,
            method: 'put',
            data,
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false}));
                } else {
                    setState(preState => ({...preState, loading: true, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: true, error: error.message}));
            });
    };
    return [state, requestData] as const;
};
