import { Dimensions, Platform } from "react-native";

const calcDiagonal = (w: number, h: number) => {
  return Math.sqrt(Math.pow(w, 2) + Math.pow(h, 2));
};

const calcScale =
  (calc: (size: number, size2?: number) => number) =>
  (size: number, size2?: number) => {
    return Math.round(calc(size, size2)) || (size > 0 ? 0.5 : 0);
  };

export const Platform_iOS = Platform.OS === "ios";
export const Platform_Android = Platform.OS === "android";
export const Dimensions_Window = Dimensions.get("window");

const mobileW = 390; //iPhone mini
const mobileH = (mobileW / 9) * (Platform_Android ? 18.5 : 19.5);
const mobileD = calcDiagonal(mobileW, mobileH);

const tabletW = 744; //iPad Mini
const tabletH = 1133;
const tabletD = calcDiagonal(tabletW, tabletH);

const { width: w, height: h } = Dimensions_Window;
const d = calcDiagonal(w, h);

export const Platform_Tablet = w >= tabletW || h >= tabletH || d >= tabletD;

const horizontalScale = (size: number, size2?: number) => {
  if (w >= tabletW) return ((size2 || size) * w) / tabletW;
  return (size * w) / mobileW;
};

const horizontalScaleCounter = (size: number, size2?: number) => {
  if (w >= tabletW) return (size2 || size) / w / tabletW;
  return size / w / mobileW;
};

const verticalScale = (size: number, size2?: number) => {
  if (h >= tabletH) return ((size2 || size) * h) / tabletH;
  return (size * h) / mobileH;
};

const verticalScaleCounter = (size: number, size2?: number) => {
  if (h >= tabletH) return (size2 || size) / h / tabletH;
  return size / h / mobileH;
};

const diagonalScale = (size: number, size2?: number) => {
  if (d >= tabletD) return ((size2 || size) * d) / tabletD;
  return (size * d) / mobileD;
};

const diagonalScaleCounter = (size: number, size2?: number) => {
  if (d >= tabletD) return (size2 || size) / d / tabletD;
  return size / d / mobileD;
};

export const hs = calcScale(horizontalScale);

export const hsc = calcScale(horizontalScaleCounter);

export const vs = calcScale(verticalScale);

export const vsc = calcScale(verticalScaleCounter);

export const ds = calcScale(diagonalScale);

export const dsc = calcScale(diagonalScaleCounter);
