import { pond } from "protobuf-ts/pond";
import { capitalize } from "utils/strings";
import { or } from "utils";
import GrainDescriber from "grain/GrainDescriber";

//TODO handle new keys for pre/post seed
const keyTranslator = new Map<keyof pond.HarvestPlanSettings, string>([
  ["createDate", "Create Date"],
  ["actualYield", "Actual Yield"],
  ["cropType", "Crop Type"],
  ["field", "Field"],
  ["grainType", "grain"],
  ["yieldTarget", "Yield Target"],
  ["bushelPrice", "Price per Bushel"],
  ["preSeedMaterials", "Pre-seed Material Cost (acre)"],
  ["preSeedEquipment", "Pre-seed Equipment Cost (acre)"]
]);

// Keys will be stringified by default if not found in the keyTranslator
export function TranslateKey(key: keyof pond.HarvestPlanSettings): string {
  let translatedKey = keyTranslator.get(key);
  return translatedKey ? translatedKey : capitalize(key.toString());
}

const valueTranslator = new Map<
  keyof pond.HarvestPlanSettings,
  (device: pond.HarvestPlanSettings) => string
>([
  ["createDate", device => new Date(device.createDate).toString()],
  ["cropType", device => GrainDescriber(device.cropType).name]
]);

// Values will be stringified by default if its key is not found in the valueTranslator
export function TranslateValue(
  key: keyof pond.HarvestPlanSettings,
  device: pond.HarvestPlanSettings
) {
  let translatorFunc = valueTranslator.get(key);
  let value: any = or(device[key], "");
  let d: string;
  d = or(value.toString(), "");
  return translatorFunc ? translatorFunc(device) : device[key] ? capitalize(d) : "";
}
