/** @format */

import * as React from 'react';
import Axios from '../utils/xhr';
import {
    PUT_INTERFACE_ENABLE,
    PUT_INTERFACE_REPORT,
    PUT_INTERCASE_WARNING,
    DELETE_INTERFACE,
    GET_INTERFACE_SOURCE_LIST,
    POST_INTERFACE_SOURCE,
} from '../const/path';

/**
 * 设置接口是否启用
 */
export const usePutInterfaceEnable = (params: {checked: boolean}) => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
        ...params,
    });
    const requestData = (id: number, enable: boolean) => {
        setState(preState => ({...preState, loading: true}));
        return Axios({
            url: PUT_INTERFACE_ENABLE + '/' + id,
            method: 'PUT',
            data: {
                enable,
            },
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false, checked: !preState.checked}));
                } 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 usePutInterfaceReport = (params: {isReport: boolean}) => {
    const [state, setState] = React.useState({
        loading: false,
        ...params,
    });
    const requestData = (id: number, is_show: boolean) => {
        setState(preState => ({...preState, loading: true}));
        Axios({
            url: PUT_INTERFACE_REPORT + '/' + id,
            method: 'PUT',
            data: {
                is_show,
            },
        })
            .then(({code}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false, isReport: !preState.isReport}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false}));
            });
    };
    return [state, requestData] as const;
};

export interface putWarnningProps {
    threshold_fail_rate: number;
    threshold_norecord_rate: number;
    threshold_timeout: number;
    warning_enable: boolean;
    rate_limit: number;
    id: number;
}
interface putWarnningState {
    loading: boolean;
    error: null | string;
}
/**
 * 预警设置
 */
export const useUpdateAlertSet = () => {
    const [state, setState] = React.useState<putWarnningState>({
        loading: false,
        error: null,
    });
    const requestData = (params: putWarnningProps) => {
        setState(preState => ({...preState, loading: true}));
        const {id, ...rest} = params;
        return Axios({
            url: PUT_INTERCASE_WARNING + '/' + params.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;
};
/**
 * 删除接口
 */
export const useDelete = () => {
    const [state, setState] = React.useState({
        loading: false,
    });
    const requestData = (id: number) => {
        setState(preState => ({...preState, loading: true}));
        return Axios({
            url: DELETE_INTERFACE + '?api_id=' + id,
            method: 'DELETE',
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState({loading: false});
                } else {
                    setState({loading: false});
                    return msg;
                }
            })
            .catch(error => {
                setState({loading: false});
                return error.message;
            });
    };
    return [state, requestData] as const;
};
/**
 * 注意: 接口 是由若干个数据源组成的一个混合接口
 */

interface ReciveProvideApiProps {
    platform_name: string;
    priority: number;
    thirdpart_api_name: string;
    thirdpart_api_router: string;
    provider_api_id: number;
}
interface ReciveSourceGroupProps {
    group_name: string;
    group_no: number;
    id: number;
    provider_api_list: ReciveProvideApiProps[];
}
export interface ProvideApiRecordProps extends ReciveProvideApiProps {
    id: number;
    key: number;
    title: string;
    parentId: number;
}
export interface GroupProps {
    group_name: string;
    group_no: number;
    id: number;
    provider_api_list: ProvideApiRecordProps[];
}
interface IListState {
    loading: boolean;
    error: null | string;
    dataSource: GroupProps[];
    deepData: {
        id?: number;
    };
}
/**
 * 获取接口下面的数据源列表
 */
export const useInterfaceSourceList = (id: number) => {
    const [state, setState] = React.useState<IListState>({
        loading: false,
        error: null,
        dataSource: [],
        deepData: {
            id: id,
        },
    });
    const requestData = (id?: number) => {
        const combineParasm = {...state.deepData, id: id};
        setState(preState => ({...preState, loading: true, error: null, deepData: combineParasm}));
        Axios({
            url: GET_INTERFACE_SOURCE_LIST,
            params: {
                api_id: combineParasm.id,
            },
        })
            .then(({code, msg, data}) => {
                if (code === 0) {
                    if (!data) {
                        data = [];
                    }
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        dataSource: data
                            .map((element: ReciveSourceGroupProps) => {
                                return {
                                    ...element,
                                    id: element.group_no,
                                    provider_api_list: element.provider_api_list
                                        .map(item => {
                                            return {
                                                ...item,
                                                id: item.provider_api_id,
                                                parentId: element.group_no,
                                            };
                                        })
                                        .sort((pre, next) => pre.priority - next.priority),
                                };
                            })
                            .sort((pre: GroupProps, next: GroupProps) => pre.group_no - next.group_no),
                    }));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
            });
    };
    React.useEffect(() => {
        requestData(id);
    }, []);
    return [state, requestData] as const;
};
interface UpdateInterfaceByProvideApiParams {
    id: number;
    data: {group_no: number; group_name: string; provider_api_id_list: number[]}[];
}

/**
 * 更新接口通过组成的数据源
 */
export const useUpdateInterfaceByProvideApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: UpdateInterfaceByProvideApiParams) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: POST_INTERFACE_SOURCE,
            method: 'POST',
            data: {
                api_id: params.id,
                provider_group_list: params.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;
};
