<?php

$fromUnit = $_GET["fu"] ?? null;
$toUnit   = $_GET["tu"] ?? null;
$fromVal  = $_GET["fv"] ?? null;
$err = null;
$result = null;

function convertValue( $val, $from, $to ) {
  if ( $from == "qrt" ) {
    switch ( $to ) {
    case "qrt":
      return $val;
    case "cup":
      return $val * 4;
    case "tbsp":
      return $val * 64;
    case "tsp":
      return $val * 192;
    case "oz":
      return $val * 32;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "cup" ) {
    switch ( $to ) {
    case "cup":
      return $val;
    case "qrt":
      return $val / 4;
    case "tbsp":
      return $val * 16;
    case "tsp":
      return $val * 48;
    case "oz":
      return $val * 8;
    case "lb":
      return $val * 8 / 16;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "tbsp" ) {
    switch ( $to ) {
    case "qrt":
      return $val / 64;
    case "cup":
      return $val / 16;
    case "tbsp":
      return $val;
    case "tsp":
      return $val * 3;
    case "oz":
      return $val / 2;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "tsp" ) {
    switch ( $to ) {
    case "qrt":
      return $val / 192;
    case "cup":
      return $val / 48;
    case "tbsp":
      return $val / 3;
    case "tsp":
      return $val;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "in" ) {
    switch ( $to ) {
    case "in":
      return $val;
    case "cm":
      return $val * 2.54;
    case "ft":
      return $val / 12;
    case "m":
      return $val * 0.254;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "cm" ) {
    switch ( $to ) {
    case "cm":
      return $val;
    case "in":
      return $val / 2.54;
    case "ft":
      return $val / 30.84;
    case "m":
      return $val / 100;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "ft" ) {
    switch ( $to ) {
    case "in":
      return $val * 12;
    case "cm":
      return $val * 30.84;
    case "ft":
      return $val;
    case "m":
      return $val / 3.28084;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "m" ) {
    switch ( $to ) {
    case "in":
      return $val / 0.254;
    case "cm":
      return $val * 100;
    case "ft":
      return $val * 3.28084;
    case "m":
      return $val;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "lb" ) {
    switch ( $to ) {
    case "lb":
      return $val;
    case "g":
      return $val * 453.592;
    case "oz":
      return $val * 16;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "g" ) {
    switch ( $to ) {
    case "lb":
      return $val / 453.592;
    case "g":
      return $val;
    case "oz":
      return $val * 0.035274 ;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "oz" ) {
    switch ( $to ) {
    case "lb":
      return $val / 16;
    case "g":
      return $val / 0.035274;
    case "oz":
      return $val;
    case "cup":
      return $val / 8;
    case "qrt":
      return $val / 32;
    case "tbsp":
      return $val * 2;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "c" ) {
    switch ( $to ) {
    case "c":
      return $val;
    case "f":
      return ($val * 9 / 5) + 32;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else if ( $from == "f" ) {
    switch ( $to ) {
    case "f":
      return $val;
    case "c":
      return ($val - 32) * 5 / 9;
    default:
      $err = "The given units cannot be converted by this converter";
      return "ERROR";
    }
  } else {
    $err = "Invalid conversion";
    return 0;
  }
}


$units = [
  "qrt" => 1,
  "cup" => 1,
  "tbsp" => 1,
  "tsp" => 1,
  "in" => 2,
  "cm" => 2,
  "ft" => 2,
  "m" => 2,
  "lb" => 3,
  "g" => 3,
  "oz" => 3,
  "c" => 4,
  "f" => 4,
];

if ( $fromUnit && $toUnit && $fromVal ) {
  if ( array_key_exists( $fromUnit, $units) && array_key_exists( $toUnit, $units ) ) {
    $result = convertValue( $fromVal, $fromUnit, $toUnit );
  } else {
    $err = "The url has been tampered with and set to an invalid value";
  }
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>sloum : food : unit conversion</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/styles.css">
    <style>
      #err-msg{padding:1em;background-color:#ff9e9e;color:#111;border-radius:5px;margin:1em auto}
      form{margin-top:4em;text-align:center}
      label{display:block;margin-bottom:1.2em}
      output{font-weight:bold;font-size:1.2em}
      input,select{font-size:1.1em;padding:5px}
    </style>
  </head>
  <body>
    <header>
      <h1 class="title">sloum</h1>
      <nav>
        <ul>
          <li><a href="/about.html">About</a></li>
          <li><a href="/food/index.html">Food</a></li>
          <li><a href="/software/index.html">Software</a></li>
          <li><a href="/fiber-arts/index.html">Fiber Arts</a></li>
          <li><a href="/projects/index.html">Projects</a></li>
          <li><a href="/music.html">Music</a></li>
          <li><a href="/reading.html">Reading</a></li>
          <li><a href="/links.html">Places</a></li>
        </ul>
      </nav>
      <hr>
    </header>
		<main>
			<h1>food : unit conversion</h1>
      <p>I often want to convert units but don't want a bunch of javascript and bloat. So I made this unit converter. It doesn't convert everything, but converts a lot of things I commonly need. Feel free to use it if you find it helpful.</p>
      <form action="convert.php" method="get" id="calc-form" <?php echo $err ? 'aria-describedby="err-msg"' : "" ?>>
        <?php
        if ( $err ) {
          echo '<p id="err-msg">Error: ' . $err . '</p>';
        }
        ?>
          <label>Current Value <input id="fv" type="number" min="0"  name="fv" step="0.001" value="<?php echo $fromVal ? $fromVal : "0.0" ?>" required></label>
        <label>Current Unit
          <select name="fu" id="fu" required>
          <option value="qrt" <?php echo $fromUnit == "qrt" ? "selected" : ""; ?>>quarts</option>
            <option value="cup" <?php echo $fromUnit == "cup" ? "selected" : ""; ?>>cups</option>
            <option value="tbsp" <?php echo $fromUnit == "tbsp" ? "selected" : ""; ?>>tablespoons</option>
            <option value="tsp" <?php echo $fromUnit == "tsp" ? "selected" : ""; ?>>teaspoons</option>
            <option value="in" <?php echo $fromUnit == "in" ? "selected" : ""; ?>>inches</option>
            <option value="cm" <?php echo $fromUnit == "cm" ? "selected" : ""; ?>>centimeters</option>
            <option value="ft" <?php echo $fromUnit == "ft" ? "selected" : ""; ?>>feet</option>
            <option value="m" <?php echo $fromUnit == "m" ? "selected" : ""; ?>>meters</option>
            <option value="lb" <?php echo $fromUnit == "lb" ? "selected" : ""; ?>>ounds</option>
            <option value="g" <?php echo $fromUnit == "g" ? "selected" : ""; ?>>grams</option>
            <option value="oz" <?php echo $fromUnit == "oz" ? "selected" : ""; ?>>ounces</option>
            <option value="c" <?php echo $fromUnit == "c" ? "selected" : ""; ?>>celsius</option>
            <option value="f" <?php echo $fromUnit == "f" ? "selected" : ""; ?>>farenheit</option>
          </select>
        </label>
        <label>New Unit
          <select name="tu" id="tu" required>
            <option value="qrt" <?php echo $toUnit == "qrt" ? "selected" : ""; ?>>quarts</option>
            <option value="cup" <?php echo $toUnit == "cup" ? "selected" : ""; ?>>cups</option>
            <option value="tbsp" <?php echo $toUnit == "tbsp" ? "selected" : ""; ?>>tablespoons</option>
            <option value="tsp" <?php echo $toUnit == "tsp" ? "selected" : ""; ?>>teaspoons</option>
            <option value="in" <?php echo $toUnit == "in" ? "selected" : ""; ?>>inches</option>
            <option value="cm" <?php echo $toUnit == "cm" ? "selected" : ""; ?>>centimeters</option>
            <option value="ft" <?php echo $toUnit == "ft" ? "selected" : ""; ?>>feet</option>
            <option value="m" <?php echo $toUnit == "m" ? "selected" : ""; ?>>meters</option>
            <option value="lb" <?php echo $toUnit == "lb" ? "selected" : ""; ?>>pounds</option>
            <option value="g" <?php echo $toUnit == "g" ? "selected" : ""; ?>>grams</option>
            <option value="oz" <?php echo $toUnit == "oz" ? "selected" : ""; ?>>ounces</option>
            <option value="c" <?php echo $toUnit == "c" ? "selected" : ""; ?>>celsius</option>
            <option value="f" <?php echo $toUnit == "f" ? "selected" : ""; ?>>farenheit</option>
          </select>
        </label>
        <input type="submit" value="submit">
        <?php if ( !is_null( $result ) && !$err ) {
        echo '<p>Result: <output id="calc-form" for="fv fu tu">' . $result . '</output></p>';
        } ?>
      </form>
    </main>
  </body>
</html>
