/** @format */
import * as React from 'react';
import Axios from '../utils/xhr';
import {GET_PROVIDER_STATISTICS_REPORT, GET_PROVIDE_USAGE_STATISTICS} from '../const/path';

interface ProviderParams {
    page_size?: number;
    is_all?: boolean;
    page_number: number;
    provider_api_id: '' | number;
    start_timestamp: number;
    end_timestamp: number;
}
interface ReciveProviderStatisticsRecordProps {
    date: string;
    fail: number;
    fail_rate: number;
    provider_api_name: string;
    provider_name: string;
    query: number;
    query_rate: string;
    success: number;
    total: number;
    un_query: number;
}
export interface ProviderStatisticsRecordProps extends ReciveProviderStatisticsRecordProps {
    index: number;
    id: number;
}
interface ProvoderStatisticsReportState {
    loading: boolean;
    error: null;
    dataSource: ProviderStatisticsRecordProps[];
    total: number;
    deepData: ProviderParams;
}
/**
 * 数据源统计报表(控制面板-统计报表-数据源查询统计)
 */
export const useProvideStatisticsReport = (params: ProviderParams) => {
    const [state, setState] = React.useState<ProvoderStatisticsReportState>({
        loading: false,
        error: null,
        dataSource: [],
        total: 0,
        deepData: {
            page_size: 10,
            is_all: false,
            ...params,
        },
    });
    const requestData = (params?: Partial<ProviderParams>) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({...preState, error: null, loading: true, deepData: combineParams}));
        Axios({url: GET_PROVIDER_STATISTICS_REPORT, params: combineParams})
            .then(({code, msg, data}) => {
                if (code === 0) {
                    if (!data.log_query_thirdpart_interface_count) {
                        data.log_query_thirdpart_interface_count = [];
                    }
                    const basePage = (data.page_number - 1) * data.page_size + 1;
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        total: data.total,
                        dataSource: data.log_query_thirdpart_interface_count.map(
                            (element: ReciveProviderStatisticsRecordProps, index: number) => {
                                return {
                                    ...element,
                                    id: basePage + index,
                                    index: basePage + index,
                                };
                            },
                        ),
                    }));
                } else {
                    setState(preState => ({...preState, loading: false, msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
            });
    };
    React.useEffect(() => {
        requestData(params);
    }, []);
    return [state, requestData] as const;
};
interface ProvideUsageStatisticsParams {
    provider_api_id: '' | number;
    start_timestamp: number;
    end_timestamp: number;
}
/**
 * 数据源用量统计(控制面板-统计报表-数据源查询统计)
 */
export const useProvideUsageStatistics = (params: ProvideUsageStatisticsParams) => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
        deepData: params,
        dataSource: {
            left_rate: 0,
            left_count: 0,
            use_count: 0,
        },
    });
    const requestData = (params?: ProvideUsageStatisticsParams) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({...preState, loading: true, error: null, deepData: combineParams}));
        Axios({url: GET_PROVIDE_USAGE_STATISTICS, params: combineParams})
            .then(({code, msg, data}) => {
                if (code === 0) {
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        dataSource: {...data, left_rate: data.left_count === 0 ? 0 : data.left_count / data.total},
                    }));
                } 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 ResultParams {
    provide_api_id: number;
    start_timestamp: number;
    end_timestamp: number;
    status: 0 | 1 | 2;
}
interface ReciveResultRecordProps {
    code: string;
    count: number;
    msg: string;
}
interface ResultRecorProps extends ReciveResultRecordProps {
    fildName: string;
    value: number;
}
interface ResultState {
    loading: boolean;
    error: null;
    deepData: ResultParams;
    dataSource: ResultRecorProps[];
}
/**
 * 控制面板-统计报表-供应商 结果分析
 */
export const useProvideResultAnalysis = () => {
    const [state, setState] = React.useState<ResultState>({
        loading: false,
        dataSource: [],
        deepData: {
            provide_api_id: 0,
            start_timestamp: 0,
            end_timestamp: 0,
            status: 0,
        },
        error: null,
    });
    const requestData = (params?: ResultParams) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({...preState, loading: true, error: null, deepData: combineParams}));
        Axios({
            url: '/access_log/third_party_err_analyze',
            params: combineParams,
        })
            .then(({code, msg, data}) => {
                if (code === 0) {
                    data = data || [];
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        dataSource: data.map((element: ReciveResultRecordProps) => {
                            return {
                                ...element,
                                value: element.count,
                                fildName: element.code,
                            };
                        }),
                    }));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
            });
    };
    const clear = React.useCallback(() => {
        setState(preState => ({
            loading: false,
            dataSource: [],
            deepData: {
                provide_api_id: 0,
                start_timestamp: 0,
                end_timestamp: 0,
                status: 0,
            },
            error: null,
        }));
    }, []);
    return [state, requestData, clear] as const;
};
