import * as React from "react";
import {Platform, StyleProp, Text, TextStyle} from 'react-native';
import app, {ThemeCss} from "../../app";
import {IProps} from "./props";
import _hooks from "./_hooks";


const BasicText: React.FC<IProps> = (props) => {
    const css = app.theme.useGet<ThemeCss>();

    const {
        onPressTime, notPressTime,
    } = _hooks.useData(props);

    //  默认样式
    const defaultStyle: StyleProp<TextStyle> = {color: css.font.color, fontSize: 14, lineHeight: 20};
    if (Platform.OS === 'android') defaultStyle.fontFamily = 'lucida grande, tahoma, verdana, arial, sans-serif';

    //  操作样式
    const styleCss: StyleProp<TextStyle> = {};
    if (props.color) styleCss.color = props.color;
    if (props.size) styleCss.fontSize = props.size;
    if (props.width) styleCss.width = props.width;
    if (props.height) styleCss.height = props.height;
    if (props.weight) styleCss.fontWeight = props.weight;
    if (props.lineHeight) styleCss.lineHeight = props.lineHeight;
    if (props.textAlign) styleCss.textAlign = props.textAlign;
    if (props.t) styleCss.marginTop = props.t;
    if (props.b) styleCss.marginBottom = props.b;
    if (props.l) styleCss.marginLeft = props.l;
    if (props.r) styleCss.marginRight = props.r;
    if (props.p) styleCss.padding = props.p;
    if (props.pv) styleCss.paddingVertical = props.pv;
    if (props.ph) styleCss.paddingHorizontal = props.ph;
    if (props.pt) styleCss.paddingTop = props.pt;
    if (props.pb) styleCss.paddingBottom = props.pb;
    if (props.pl) styleCss.paddingLeft = props.pl;
    if (props.pr) styleCss.paddingRight = props.pr;
    if (props.m) styleCss.margin = props.m;
    if (props.mv) styleCss.marginVertical = props.mv;
    if (props.mh) styleCss.marginHorizontal = props.mh;
    if (props.mt) styleCss.marginTop = props.mt;
    if (props.mb) styleCss.marginBottom = props.mb;
    if (props.ml) styleCss.marginLeft = props.ml;
    if (props.mr) styleCss.marginRight = props.mr;

    if (typeof props.onPress === "function" || typeof props.onLongPress === "function" || props.isSelectable) {
        return <Text
            style={[defaultStyle, props.style, styleCss]}
            //  是否可以选择复制
            selectable={props.isSelectable}
            selectionColor={props.selectableColor}
            //  控制字体是否要根据系统的“字体大小”辅助选项来进行缩放。默认值为true。
            allowFontScaling={false}
            //  用来当文本过长的时候裁剪文本。包括折叠产生的换行在内，总的行数不会超过这个属性的限制。
            numberOfLines={props.line}
            onPress={event => {
                const time = new Date().getTime();
                if (onPressTime.current > time) return;
                onPressTime.current = (time + notPressTime);
                if (typeof props.onPress === "function") props.onPress(event);
            }}
            onLongPress={event => {
                if (typeof props.onLongPress === "function") props.onLongPress(event);
            }}
        >
            {props.children ?? props.text ?? ''}
        </Text>
    }

    return <Text
        style={[defaultStyle, props.style, styleCss]}
        //  控制字体是否要根据系统的“字体大小”辅助选项来进行缩放。默认值为true。
        allowFontScaling={false}
        //  用来当文本过长的时候裁剪文本。包括折叠产生的换行在内，总的行数不会超过这个属性的限制。
        numberOfLines={props.line}>
        {props.children ?? props.text ?? ''}
    </Text>
}

export default BasicText