import {Dimensions, PixelRatio} from 'react-native';
import {IS_TV} from './tv';

const {width: SCREEN_WIDTH, height: SCREEN_HEIGHT} = Dimensions.get('window');

// Phone vs tablet/TV breakpoint
export const IS_PHONE = SCREEN_WIDTH < 600;

// Re-export for convenience
export {IS_TV};

// Width of the permanent side navigation bar (hidden on phone)
export const SIDEBAR_W = IS_PHONE ? 0 : 64;

export const dimensions = {
  screenWidth: SCREEN_WIDTH,
  screenHeight: SCREEN_HEIGHT,

  // Card sizes
  posterWidth: {
    small: 90,
    medium: 120,
    large: 150,
  },
  posterHeight: {
    small: 135,  // 2:3 ratio
    medium: 180,
    large: 225,
  },

  // Grid
  gridColumns: 3,
  gridSpacing: 8,

  // Layout
  headerHeight: 56,
  tabBarHeight: 60,
  heroHeight: SCREEN_HEIGHT * 0.55,

  // Border radius
  cardRadius: 8,
  buttonRadius: 6,
  modalRadius: 16,

  // Padding/Margin
  pagePadding: 16,
  itemSpacing: 12,
  rowSpacing: 24,
};

export function normalize(size: number): number {
  const scale = SCREEN_WIDTH / 375;
  const newSize = size * scale;
  return Math.round(PixelRatio.roundToNearestPixel(newSize));
}

export function getPosterDimensions(
  shape: 'regular' | 'square' | 'landscape' = 'regular',
  size: 'small' | 'medium' | 'large' = 'medium',
) {
  const width = dimensions.posterWidth[size];
  switch (shape) {
    case 'square':
      return {width, height: width};
    case 'landscape':
      return {width: width * 1.78, height: width};
    default:
      return {width, height: dimensions.posterHeight[size]};
  }
}
