/**
 *  @format
 * 数据API 查询方式配置
 * */

import * as React from 'react';
import Axios from '../utils/xhr';
import {addId} from '../utils/operation';
import {
    DELETE_COMBO,
    POST_COMBO,
    GET_QUERY_TYPE_LIST,
    GET_QUERYTYPE_DETAIL,
    PUT_QUERY_TYPE,
    POST_QUERY_TYPE_API,
    DELETE_QUERY_TYPE,
} from '../const/path';
/**
 * 删除配额记录
 */
export const useDeleteCombo = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: {combo_id: number; query_type_id: number}) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({url: DELETE_COMBO, method: 'DELETE', data: params})
            .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 useInsertCombo = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: {count: number; price: number; type: 1 | 2; query_type_id: number}[]) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: POST_COMBO,
            method: 'POST',
            data: {
                management_add_data_combo_list: params,
            },
        })
            .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 interface ReciveQuryeTypeProps {
    query_type_name: string;
    query_type_id: number;
}
export interface IQueryTypeProps extends ReciveQuryeTypeProps {
    id: number;
    key: number;
    label: string;
    value: number;
}
interface QueyTypeListState {
    loading: boolean;
    error: null | string;
    dataSource: IQueryTypeProps[];
}
/**
 * 获取数据API下面的查询方式列表
 */
export const useQueryTypeList = () => {
    const [state, setState] = React.useState<QueyTypeListState>({
        loading: false,
        error: null,
        dataSource: [],
    });
    const requestData = (id: number) => {
        setState(preState => ({...preState, loading: true, error: null}));
        Axios({
            url: GET_QUERY_TYPE_LIST,
            params: {
                data_api_id: id,
            },
        })
            .then(({code, msg, data}) => {
                if (code === 0) {
                    if (!data.query_type_list) {
                        data.query_type_list = [];
                    }
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        dataSource: data.query_type_list.map((element: ReciveQuryeTypeProps) => {
                            return {
                                id: element.query_type_id,
                                query_type_name: element.query_type_name,
                                query_type_id: element.query_type_id,
                                key: element.query_type_id,
                                label: element.query_type_name,
                                value: element.query_type_id,
                            };
                        }),
                    }));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
            });
    };

    return [state, requestData] as const;
};
interface UpdateAndPostQUeryTypes {
    data_api_id: number;
    query_types: {id: number; query_type_name: string}[];
}
/**
 * 新增查询方式或者修改查询方式名字
 */
export const useUpdateQueryModo = () => {
    const [state, setState] = React.useState({loading: false, error: null});
    const requestData = (data: UpdateAndPostQUeryTypes) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_QUERY_TYPE,
            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;
};
/**
 * 删除查询方式
 */
export const useDeleteQueryType = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (id: number) => {
        setState(preState => ({...preState, loading: true, error: null}));

        return Axios({
            url: DELETE_QUERY_TYPE,
            method: 'DELETE',
            params: {
                query_type_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;
};
interface RecivequeryIntrefaceProps {
    api_id: number;
    api_name: string;
    api_type: number;
    data_api_base_api_id: number;
    priority: number;
    router: string;
    sale_name: string;
}
export interface IqueryIntrefaceProps extends RecivequeryIntrefaceProps {
    id: number;
}
interface QueryTypeIntrefaceState {
    loading: boolean;
    dataSource: IqueryIntrefaceProps[];
    error: null | string;
}

/**
 * 数据服务系统-数据API列表-查询方式-添加接口
 */
export const useInsertInterfaceOnQueryType = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: {base_api_list: number[]; query_type_id: number}) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: POST_QUERY_TYPE_API,
            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;
};

export interface ReciveQueryTypeProviderProps {
    enable: false;
    platform_name: string;
    priority: number;
    provider_api_id: number;
    thirdpart_api_name: string;
}
export interface ReciveProvidergrouplistProps {
    group_name: string;
    group_no: number;
    provider_api_list: ReciveQueryTypeProviderProps[];
}

export interface DataApiQueryTypeDetailProps {
    is_free: boolean;
    free_total: number;
    unit_price: number;
    total_data_combos: {
        combo_id: number;
        type: number;
        count: number;
        price: number;
        query_type_id: number;
        total_day: number;
    }[];
    free_data_combos: {
        combo_id: number;
        type: number;
        count: number;
        price: number;
        query_type_id: number;
        total_day: number;
    }[];
    day_data_combos: {
        combo_id: number;
        type: number;
        count: number;
        price: number;
        query_type_id: number;
        total_day: number;
    }[];
    data_api_base_apis?: {
        id: number;
        api_id: number;
        api_name: string;
        count_code: string;
        count_type: 0 | 1;
        filters: {name: string; value: string}[];
        force_update: boolean;
        is_crypto: boolean;
        request_params: [];
        response_params: [];
        sale_name: string;
        reuse_time: number;
        router: string;
        provider_group_list: ReciveProvidergrouplistProps[];
    }[];
}
interface QueryModeDetailState {
    loading: boolean;
    error: null | string;
    dataSource: DataApiQueryTypeDetailProps;
}
/**
 * 获取数据API 下的查询方式详情(包括了 包含的baseApi列表)
 */
export const useDataApiQueryTypeDetail = (id: number) => {
    const [state, setState] = React.useState<QueryModeDetailState>({
        loading: false,
        error: null,
        dataSource: {
            data_api_base_apis: [],
            free_total: 0,
            unit_price: 0,
            is_free: false,
            day_data_combos: [],
            total_data_combos: [],
            free_data_combos: [],
        },
    });
    const requestData = (id: number) => {
        setState(preState => ({...preState, loading: true, error: null}));
        Axios({
            url: GET_QUERYTYPE_DETAIL,
            params: {
                query_type_id: id,
            },
        })
            .then(({code, msg, data}) => {
                if (code === 0) {
                    data.data_api_base_apis = data.data_api_base_apis || [];
                    data.total_data_combos = data.total_data_combos || [];
                    data.day_data_combos = data.day_data_combos || [];
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        dataSource: {
                            ...data,
                            total_data_combos: data.total_data_combos.map(element => {
                                return {
                                    ...element,
                                    id: element.combo_id,
                                };
                            }),
                            day_data_combos: data.day_data_combos.map(element => {
                                return {
                                    ...element,
                                    id: element.combo_id,
                                };
                            }),
                            data_api_base_apis: data.data_api_base_apis.map(element => {
                                element.provider_group_list = element.provider_group_list || [];
                                element.filters = element.filters || [];
                                element.request_params = element.request_params || [];
                                return {
                                    ...element,
                                    id: element.data_api_base_api_id,
                                    api_id: element.api_id,
                                    request_params: addId(element.request_params, 'request'),
                                    filters: element.filters.map((item, idx) => {
                                        return {...item, id: idx + 1};
                                    }),
                                    provider_group_list: element.provider_group_list.map(item => {
                                        return {
                                            ...item,
                                            id: item.group_no,
                                            provider_api_list: item.provider_api_list.map(each => {
                                                return {
                                                    ...each,
                                                    id: each.provider_api_id,
                                                };
                                            }),
                                        };
                                    }),
                                };
                            }),
                        },
                    }));
                } 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;
};
/**
 * 更新查询方式的收费情况
 */
export const useUpdateQueryType = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: {count: number; is_free: boolean; query_type_id: number; unit_price: number}) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: '/data_api/set_free_combo',
            data: params,
            method: 'PUT',
        })
            .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;
};
