/** @format */

import * as React from 'react';
import Axios from '../utils/xhr';
import {
    GET_PROVIDES_LIST,
    PUT_DATASOURCE,
    GET_PROVIDER_PLATFORM_LIST,
    POST_DATASOURCE,
    DELETE_DATASOURCE,
} from '../const/path';

export interface ReceiveProviderProps {
    provider_id: number;
    platform_name: string;
    contact: string;
    email: string;
    cost: string;
    create_time: string;
    platform_code: string;
    update_time: string;
}
export interface ProviderProps extends ReceiveProviderProps {
    label: string;
    key: number;
    value: number;
    id: number;
}

const page_size = 10;

interface ProviderParams {
    is_all: boolean;
    search?: string;
    page_size?: number;
    page_number: number;
    provider_api_id?: number | string;
}

interface ProviderState {
    loading: boolean;
    error: null | string;
    dataSource: ProviderProps[];
    total: number;
    deepData: {
        is_all: boolean;
        page_size?: number;
        page_number?: number;
        search?: string;
    };
}

/**
 * 数据源 获取所有供应商
 */
export const useProviders = () => {
    const [state, setState] = React.useState<ProviderState>({
        loading: false,
        error: null,
        dataSource: [],
        total: 0,
        deepData: {
            is_all: false,
            page_size: page_size,
            page_number: 1,
            search: '',
        },
    });
    const requestData = (params?: Partial<ProviderParams>) => {
        const combinParams = params ? {...state.deepData, ...params} : state.deepData;
        setState(preState => ({...preState, loading: true, error: null, deepData: combinParams}));
        Axios({
            url: GET_PROVIDES_LIST,
            params: combinParams,
        })
            .then(({code, data, msg}) => {
                if (code === 0) {
                    if (!data.provider_platforms) {
                        data.provider_platforms = [];
                    }
                    const basePage = (data.page_number - 1) * data.page_size + 1;
                    setState(preState => {
                        return {
                            ...preState,
                            loading: false,
                            total: data.total,
                            dataSource: data.provider_platforms.map((element: ReceiveProviderProps, index: number) => {
                                return {
                                    ...element,
                                    index: basePage + index,
                                    key: element.provider_id,
                                    value: element.provider_id,
                                    id: element.provider_id,
                                    label: element.platform_name,
                                };
                            }),
                        };
                    });
                } else {
                    setState(preSate => ({...preSate, loading: false, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
            });
    };
    return [state, requestData] as const;
};

interface sourceDataProps {
    platform_name: string;
    platform_code: string;
    contact: string;
    email: string;
}
/**
 * 新增数据源
 */
export const useInsertSource = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: sourceDataProps) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: POST_DATASOURCE,
            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;
};
/**
 * 更新数据源
 */
interface ChangeProps extends sourceDataProps {
    provider_id: number;
}
export const useChangeSource = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: ChangeProps) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_DATASOURCE,
            method: 'PUT',
            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 const useDeleteSource = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (id: number) => {
        setState({
            loading: true,
            error: null,
        });
        return Axios({
            url: DELETE_DATASOURCE + '?provider_id=' + id,
            method: 'DELETE',
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState({
                        loading: false,
                        error: null,
                    });
                } else {
                    setState({
                        loading: false,
                        error: msg,
                    });
                    return msg;
                }
            })
            .catch(error => {
                setState({
                    loading: false,
                    error: error.message,
                });
                return error.message;
            });
    };
    return [state, requestData] as const;
};

interface RecivePrvideApiRecordProps {
    platform_name: string;
    provider_api_code: string;
    provider_api_id: number;
    thirdpart_api_method: string;
    thirdpart_api_name: string;
    thirdpart_api_router: string;
    thirdpart_host: string;
}
export interface ProvideApiRecordProps extends RecivePrvideApiRecordProps {
    id: number;
    title: string;
    key: number;
    label: string;
    value: string | number;
}
interface ProvideApiState {
    dataSource: ProvideApiRecordProps[];
    loading: boolean;
    error: null;
}

/**
 * 供应商-提供得接口
 * 如果未传输id则代表获取所有的数据Api
 */
export const useProvideApi = () => {
    const [state, setState] = React.useState<ProvideApiState>({
        loading: false,
        error: null,
        dataSource: [],
    });
    const requestData = (id?: number | string) => {
        setState(preState => ({...preState, loading: false}));
        Axios({
            url: GET_PROVIDER_PLATFORM_LIST,
            params: {
                provider_id: id || '',
            },
        }).then(({code, msg, data}) => {
            if (code === 0) {
                if (!data.provider_api_base_info_list) {
                    data.provider_api_base_info_list = [];
                }
                setState(preState => ({
                    ...preState,
                    loading: false,
                    dataSource: data.provider_api_base_info_list
                        .map((element: RecivePrvideApiRecordProps) => {
                            return {
                                ...element,
                                id: element.provider_api_id,
                                value: element.provider_api_id,
                                label: `${element.platform_name}(${element.thirdpart_api_name})`,
                                key: element.provider_api_id,
                                router: element.thirdpart_host,
                            };
                        })
                        .sort((pre: ProvideApiRecordProps, next: ProvideApiRecordProps) => {
                            return pre.label.localeCompare(next.label);
                        }),
                }));
            } else {
                setState(preState => ({...preState, loading: false, error: msg}));
            }
        });
    };
    return [state, requestData] as const;
};
