import ColorService, {ColorServiceImpl} from './Color.service';
import {LandType} from '../../utils/types';

export default interface BiomeService {
  checkBiomeLandType(x: number, y: number, width: number): LandType
  isMountainBiome(x: number, y: number, width: number): boolean
  isHillBiome(x: number, y: number, width: number): boolean
  isWoodedBiome(x: number, y: number, width: number): boolean
}

export class BiomeServiceImpl implements BiomeService {
  private readonly colorService: ColorService

  constructor(private readonly biomePngData: Buffer) {
    this.colorService = new ColorServiceImpl()
  }

  checkBiomeLandType(x: number, y: number, width: number): LandType {
    const dataIndex = ((y * width) + x) * 4;
    switch (this.colorService.rgb(
      this.biomePngData[dataIndex],
      this.biomePngData[dataIndex + 1],
      this.biomePngData[dataIndex + 2],
    )) {
    case 0xfa9418:
    case 0xffbc40:
      return LandType.DESERT;
    case 0x606060:
    case 0xd25f12:
    case 0xd94515:
    case 0xca8c65:
      return LandType.MESA;
    case 0x056621:
    case 0x0b6659:
    case 0x537b09:
    case 0x2c4205:
    case 0x628b17:
    case 0x507050:
    case 0x2d8e49:
    case 0x8ab33f:
    case 0x687942:
      return LandType.FOREST;
    case 0x596651:
    case 0x454f3e:
    case 0x338e81:
      // return LandType.MUD;
      // case 0xfade55:
      //   return LandType.BEACH;
    case 0xffffff:
    case 0xa0a0a0:
    case 0xfaf0c0:
    case 0x597d72:
    case 0x202070:
    case 0x202038:
    case 0x404090:
    case 0x163933:
      return LandType.ICE;
    case 0xfade55:
    case 0x8db360:
    case 0x07f9b2:
    case 0xbdb25f:
    case 0xa79d64:
    case 0xb5db88:
    case 0x000070:
    case 0x0000ff:
    case 0x0000ac:
    case 0x000090:
    case 0x000050:
    case 0x000040:
    default:
      break;
    }

    return LandType.PLAINS;
  }

  isMountainBiome(x: number, y: number, width: number): boolean {
    const dataIndex = ((y * width) + x) * 4;
    switch (this.colorService.rgb(
      (this.biomePngData[dataIndex]),
      (this.biomePngData[dataIndex + 1]),
      (this.biomePngData[dataIndex + 2]),
    )) {
    case 0x606060:
    case 0xa0a0a0:
    case 0x507050:
    case 0x338e81:
    case 0x597d72:
      return true
    default:
      break
    }

    return false
  }

  isHillBiome(x: number, y: number, width: number): boolean {
    const dataIndex = ((y * width) + x) * 4;
    switch (this.colorService.rgb(
      (this.biomePngData[dataIndex]),
      (this.biomePngData[dataIndex + 1]),
      (this.biomePngData[dataIndex + 2]),
    )) {
    case 0xd25f12:
    case 0x163933:
    case 0x2c4205:
    case 0x454f3e:
    case 0x687942:
      return true
    default:
      break
    }

    return false
  }

  isWoodedBiome(x: number, y: number, width: number): boolean {
    const dataIndex = ((y * width) + x) * 4;
    switch (this.colorService.rgb(
      (this.biomePngData[dataIndex]),
      (this.biomePngData[dataIndex + 1]),
      (this.biomePngData[dataIndex + 2]),
    )) {
    case 0x056621:
    case 0x537b09:
    case 0x596651:
    case 0x2d8e49:
      return true;
    default:
      break
    }
    return false;
  }
}
