<?php
// use Kirby\Cms\Page;

// // more validation is needed
// function addThrows($page, $site, $kirby){
//   $json["status"] = "ok";
//   $error = "";
//   if (!$page->enddate()->isEmpty()) {
//     $error = "Game ended";
//     $json["status"] = "error";
//     $json["error"] = $error;
//     return $json;
//   }
//   $throws = $kirby->request()->get("throws");
//   if (is_null($throws) || !is_array($throws)) {
//     $error = "You have to pass throws as array";
//     if ($error != "") {
//       $json["status"] = "error";
//       $json["error"] = $error;
//     }
//     return $json;
//   }
//   $checkoutTries = $kirby->request()->get("checkoutTries") ? intval($kirby->request()->get("checkoutTries")) : 0;
//   $done = $kirby->request()->get("done");
//   $numDarts = 3;
//   if ($kirby->request()->get("numDarts")) {
//     $numDarts = intval($kirby->request()->get("numDarts"));
//   } elseif (!$done) {
//     $numDarts = count($throws);
//   }
//   try {
//     $page->addThrows($throws, $numDarts, $checkoutTries, $done);
//   } catch (\Exception $e) {
//     $error = $e->getMessage();
//   }
//   if ($error != "") {
//     $json["status"] = "error";
//     $json["error"] = $error;
//   }
//   return $json;
// }

// function deleteLastThrow($page, $site, $kirby){
//   $json["status"] = "ok";
//   $error = "";
//   if (!$page->enddate()->isEmpty()) {
//     $error = "Game ended";
//     $json["status"] = "error";
//     $json["error"] = $error;
//     return $json;
//   }
//   $game = $page->rounds()->parseJSON();
//   $set = &$game["sets"][count($game["sets"])-1];
//   $leg = &$set["legs"][count($set["legs"])-1];
//   $visit = &$leg["visits"][count($leg["visits"])-1];

//   $completeVisit = $kirby->request()->get("visit");
//   if (count($visit['throws']) > 0 && !$completeVisit) {
//     array_pop($visit['throws']);
//     $kirby->impersonate('kirby');
//     $page = $page->update([
//       'rounds' => json_encode($game),
//     ]);
//   } else {
//     // delete last visit. This means, delete 2 lasts and insert one.
//     array_pop($leg["visits"]);
//     if (count($leg["visits"]) == 0){
//       array_pop($set["legs"]);
//       if (count($set["legs"]) == 0){
//         array_pop($game["sets"]);
//         if (count($game["sets"]) == 0){
//           $page = $page->initGame(false);
//           $page->recalcStats();
//           return $json;
//         }
//       }
//     }
//     $kirby->impersonate('kirby');
//     $page = $page->update([
//       'rounds' => json_encode($game),
//     ]);
//     $page = $page->clearLastVisit();
//   }
//   $page = $page->recalcStats();
//   if ($error != "") {
//     $json["status"] = "error";
//     $json["error"] = $error;
//   }
//   return $json;
// }

// function init($page, $site, $kirby){
//   $json["status"] = "ok";
//   $error = "";
//   if ($page->rounds()->toString() != "") {
//     $error = "Game already started, can not update";
//     $json["status"] = "error";
//     $json["error"] = $error;
//     return $json;
//   }
//   $players = $kirby->request()->get("players");
//   if (is_null($players) || !is_array($players) || count($players) == 0)  {
//     $error = "Players not given.";
//     $json["status"] = "error";
//     $json["error"] = $error;
//     return $json;
//   }
//   foreach ($players as $i => $player) {
//     try {
//       if (page($player)->intendedTemplate() != "member") {
//         $error = "Player is not a member ".page($player)->template();
//         $json["status"] = "error";
//         $json["error"] = $error;
//         return $json;
//       }
//     } catch (\Exception $e) {
//       $error = "Player not found".$e;
//       $json["status"] = "error";
//       $json["error"] = $error;
//       return $json;
//     }
//   }
//   $page = $page->reorderPlayer($players);
//   $page->initGame();

//   if ($error != "") {
//     $json["status"] = "error";
//     $json["error"] = $error;
//   }
//   return $json;
// }

// function update($page, $site, $kirby){
//   $json["status"] = "ok";
//   $error = "";

//   // TODO check data for valid fields
//   $kirby->impersonate('kirby');
//   $data = $kirby->request()->get("data");
//   $page = $page->update($data);

//   if ($error != "") {
//     $json["status"] = "error";
//     $json["error"] = $error;
//   }
//   return $json;
// }

// return function ($page, $site, $kirby) {
//   $error = "";
//   $json = [];
//   $type = "html";
//   if ($kirby->request()->is('POST')) {
//     $type = "json";
//     $action = $kirby->request()->get("action");
//     if ($action == "update") {
//       $json = update($page, $site, $kirby);
//     } elseif ($action == "init") {
//       $json = init($page, $site, $kirby);
//     } elseif ($action == "addThrows") {
//       $json = addThrows($page, $site, $kirby);
//     } elseif ($action == "deleteLastThrow") {
//       $json = deleteLastThrow($page, $site, $kirby);
//     } else {
//       $json["error"] = "No known action defined";
//       $json["status"] = "error";
//     }
//   }

//   return [
//     'type' => $type,
//     'json' => $json,
//     'error' => $error
//   ];
// };

function update($page, $site, $kirby){
  $json["status"] = "ok";
  $error = "";

  // TODO check data for valid fields
  $kirby->impersonate('kirby');
  $data = $kirby->request()->get("data");
  $page = $page->update($data);

  if ($error != "") {
    $json["status"] = "error";
    $json["error"] = $error;
  }
  return $json;
}

return function ($page, $site, $kirby) {
  $error = "";
  $json = [];
  $type = "html";
  if ($kirby->request()->is('POST')) {
    $type = "json";
    $action = $kirby->request()->get("action");
    if ($action == "update") {
      $json = update($page, $site, $kirby);
    } else {
      $json["error"] = "No known action defined";
      $json["status"] = "error";
    }
  }

  return [
    'type' => $type,
    'json' => $json,
    'error' => $error
  ];
};