import * as React from "react";
import {ActivityIndicator, Image, View} from "react-native";
import {reduxTools} from '@yoronsoft/js-utils';
import {configEmitterParams, configInitValue, loadingOperate, loggerConfig} from "./config";
import {
    ILoading,
    LoadingEmitterParams,
    LoadingInitParams,
    LoadingNameData,
    LoadingSmallParams, LoadingTypeData
} from "./loading";
import app from "../app";
import Basic from "../basicPage";
import {ThemeCss} from "../app";

const logger = loggerConfig('');

const loading: ILoading = {
    types: {
        system: "system",
        icon: "icon",
        img: "img",
        gif: "gif",
        other: "other"
    },
    names: {
        start: "start",
        finish: "finish",
        cancel: "cancel",
        error: "error"
    },
    init: function (params?: LoadingInitParams[]): void {
        params = params || [];
        const startData = params.find(m => m.name === this.names.start);
        const finishData = params.find(m => m.name === this.names.finish);
        const cancelData = params.find(m => m.name === this.names.cancel);
        const errorData = params.find(m => m.name === this.names.error);
        if (!startData) params.push({name: this.names.start, type: this.types.system});
        if (!finishData) params.push({name: this.names.finish, type: this.types.system});
        if (!cancelData) params.push({name: this.names.cancel, type: this.types.system});
        if (!errorData) params.push({name: this.names.error, type: this.types.system});
        reduxTools.create(configInitValue, params);
        const emitterParams: LoadingEmitterParams = {type: loadingOperate.hide, display: false};
        reduxTools.create(configEmitterParams, emitterParams);
    },
    has: function (): boolean {
        return reduxTools.get<LoadingEmitterParams>(configEmitterParams).type !== loadingOperate.hide;
    },
    show: function (content?: string, names?: LoadingNameData): void {
        names = names || this.names.start;
        const nameData = getNames(names);
        if (nameData) {
            const params: LoadingEmitterParams = {
                type: loadingOperate.show, display: true, content,
                value: getValues(nameData.type, nameData.value, nameData.style),
            }
            app.emitter.set(configEmitterParams, params);
        } else {
            logger.log(reduxTools.get(configInitValue));
            logger.err(`show(content?,names?); 未找到匹配的names`, names);
        }
    },
    hide: function (): void {
        const params: LoadingEmitterParams = {type: loadingOperate.hide, display: false};
        app.emitter.set(configEmitterParams, params);
    },
    content: function (text: string): void {
        const params: LoadingEmitterParams = {type: loadingOperate.content, content: text, display: true};
        app.emitter.set(configEmitterParams, params);
    },
    small: function (params?: LoadingSmallParams): React.ReactNode {
        params = params || {};
        return <View style={[{padding: 20}, params.style]}>
            <ActivityIndicator size={'small'} color={params.color ?? '#808080'}/>
        </View>
    }
}

const getNames = (name: LoadingNameData) => {
    const data: LoadingInitParams[] = reduxTools.get(configInitValue) ?? [];
    return data.find(m => m.name === name);
}

const getValues = (types: LoadingTypeData, value: any, style?: any) => {
    const css = app.theme.get<ThemeCss>();
    if (types === loading.types.system)
        return <ActivityIndicator size={'large'} color={css.loading.color}/>
    else if (types === loading.types.icon)
        return <Basic.Icon name={value} style={[{fontSize: 40, color: css.loading.color}, style]}/>
    else if (types === loading.types.img)
        return <Image source={value} style={[{width: 70, height: 70}, style]}/>
    else if (types === loading.types.gif)
        return <Image source={value} style={[{width: 70, height: 70}, style]}/>
    else if (types === loading.types.other) {
        return value
    } else {
        logger.warn('getValues未找到参数types', types)
        return <View/>
    }
}

export default loading
export type {
    ILoading,
    LoadingInitParams,
    LoadingNameData, LoadingTypeData,
    LoadingSmallParams,
}
