import {
    type ViewStyle,
    Dimensions,
    Platform
} from "react-native";
import type {
    ActivityStatusType,
    AllStyles
} from "../types";

const dimensions = Dimensions.get("window");

export const windowHeight = dimensions.height;
export const windowWidth = dimensions.width;

export const STATUS_CHEAT_SHEET: Record<ActivityStatusType, {
    icon: keyof NCoreUIKit.IconContentColors;
    text: keyof NCoreUIKit.TextContentColors;
}> = {
    "away": {
        icon: "warning",
        text: "warning"
    },
    "offline": {
        icon: "default",
        text: "low"
    },
    "online": {
        icon: "success",
        text: "success"
    },
    "busy": {
        icon: "danger",
        text: "danger"
    },
    "idle": {
        icon: "info",
        text: "info"
    }
};

export const webStyle = <T extends AllStyles>(styles: T): ViewStyle => {
    return (Platform.OS === "web" ? styles : {}) as unknown as ViewStyle;
};

export const uuid = () => {
    return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
        const r = Math.random() * 16 | 0, v = c == "x" ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
};

const ANDROID_FONT_FIX = {
    "200": "ExtraLight",
    "800": "ExtraBold",
    "600": "SemiBold",
    "400": "Regular",
    "500": "Medium",
    "300": "Light",
    "100": "Thin",
    "700": "Bold",
    "900": "Black"
};

export const androidTypographyFixer = (defaultPaletteData: NCoreUIKit.Palette): NCoreUIKit.Palette => {
    if(defaultPaletteData.isNeedAndroidTypographyFixer && Platform.OS === "android") {
        const spacious = defaultPaletteData.typography.spacious;
        const compact = defaultPaletteData.typography.compact;

        (Object.keys(spacious) as Array<keyof typeof spacious>).forEach((key) => {
            spacious[key] = {
                ...spacious[key],
                fontFamily: spacious[key].fontFamily.indexOf("-") === -1
                    ? `${spacious[key].fontFamily}-${ANDROID_FONT_FIX[spacious[key].fontWeight as keyof typeof ANDROID_FONT_FIX]}`
                    : spacious[key].fontFamily,
                fontWeight: "normal"
            };
        });

        (Object.keys(compact) as Array<keyof typeof compact>).forEach((key) => {
            compact[key] = {
                ...compact[key],
                fontFamily: compact[key].fontFamily.indexOf("-") === -1
                    ? `${compact[key].fontFamily}-${ANDROID_FONT_FIX[compact[key].fontWeight as keyof typeof ANDROID_FONT_FIX]}`
                    : compact[key].fontFamily,
                fontWeight: "normal"
            };
        });

        return {
            ...defaultPaletteData,
            typography: {
                spacious: spacious,
                compact: compact
            }
        };
    } else {
        return defaultPaletteData;
    }
};
