import { Platform, TextStyle } from 'react-native';

/**
 * Just the font names.
 *
 * The various styles of fonts are defined in the <Text /> component.
 */

export type FontWeight =
  | 'bold'
  | '100'
  | '200'
  | '300'
  | '400'
  | '500'
  | '600'
  | '700'
  | '800'
  | '900';

type WeightName =
  | 'thin'
  | 'extraLight'
  | 'light'
  | 'regular'
  | 'medium'
  | 'semiBold'
  | 'bold'
  | 'extraBold'
  | 'black';

const fontWeight: Record<WeightName, FontWeight> = {
  thin: '100',
  extraLight: '200',
  light: '300',
  regular: '400',
  medium: '500',
  semiBold: '600',
  bold: 'bold',
  extraBold: '800',
  black: '900',
};

export const typography = {

  //TODO
  /**
   * Different font versions
   */
  fonts: {
    thin: Platform.select({ ios: 'Helvetica', android: 'Inter-Thin' }),
    extraLight: Platform.select({ ios: 'Helvetica', android: 'Inter-ExtraLight' }),
    light: Platform.select({ ios: 'Helvetica', android: 'Inter-Light' }),
    regular: Platform.select({ ios: 'Helvetica', android: 'Inter-Regular' }),
    medium: Platform.select({ ios: 'Helvetica', android: 'Inter-Medium' }),
    semiBold: Platform.select({ ios: 'Helvetica', android: 'Inter-SemiBold' }),
    bold: Platform.select({ ios: 'Helvetica', android: 'Inter-Bold' }),
    extraBold: Platform.select({ ios: 'Helvetica', android: 'Inter-ExtraBold' }),
    black: Platform.select({ ios: 'Helvetica', android: 'Inter-Black' }),
  },

  /**
   * The available font sizes
   */
  fontSize: [8, 10, 12, 14, 16, 20, 24, 32, 40, 48, 56, 112],

  /*
   * The available font weight
   */
  fontWeight,

  /**
   * The available letter spacings
   */
  letterSpacing: [0.15, 0.2, 0.25, 0.5, 1, 1.5, 2],

  /**
   * The available line heights
   */
  lineHeight: [12, 16, 17, 19, 24, 29, 39, 48, 58, 68],
};

const WEIGHT_TO_FONT = {
  [fontWeight.thin]: typography.fonts.thin,
  [fontWeight.extraLight]: typography.fonts.extraLight,
  [fontWeight.light]: typography.fonts.light,
  [fontWeight.regular]: typography.fonts.regular,
  [fontWeight.medium]: typography.fonts.medium,
  [fontWeight.semiBold]: typography.fonts.semiBold,
  [fontWeight.bold]: typography.fonts.bold,
  [fontWeight.extraBold]: typography.fonts.extraBold,
  [fontWeight.black]: typography.fonts.black,
};

export function setFont(style: TextStyle) {
  if (Platform.OS === 'android' && style.fontFamily && style.fontFamily.includes('Inter')) {
    style.fontFamily = WEIGHT_TO_FONT[style.fontWeight] || typography.fonts.regular;
    delete style.fontWeight;
  }
  return style;
}
