<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Support\Facades\Log;

use App\Http\Controllers\AuthController;

class NihonController extends Controller {
  public function __construct() {}

  public function getGeoRegion () {
    $get = DB::table('nhn_region')->get();

    foreach ($get as $g) {
      $i = DB::table('nhn_island')->where('id', $g->island_id)->first();
      $g->island = $i;

      unset($i->id); unset($g->island_id);
    }

    return $get;
  }

  public function getGeoPrefecture () {
    $get = DB::table('nhn_prefecture')->get()->toArray();

    foreach ($get as $g) {
      $r = DB::table('nhn_region')->where('id', $g->region_id)->first();
      $i = DB::table('nhn_island')->where('id', $r->island_id)->first();
      $c = DB::table('nhn_city')  ->where('id', $g->capital)  ->first();

      $g->code = array('prefcode' => 'JP-'.sprintf("%02d", $g->id), 'countrycode' => 'JP', 'regioncode' => sprintf("%02d", $g->id));
      $g->pref = array('name' => $g->name, 'nameKana' => $g->nameKana, 'nameEng' => $g->nameEng);
      $g->type = array('name' => '県', 'nameKana' => 'けん', 'nameEng' => 'Prefecture');
      if ($g->id == 1) $g->type = array('name' => '道', 'nameKana' => 'どう', 'nameEng' => 'Territory');
      else if ($g->id == 13) $g->type = array('name' => '都', 'nameKana' => 'と', 'nameEng' => 'Metropolis');
      else if ($g->id == 26 || $g->id == 27) $g->type = array('name' => '府', 'nameKana' => 'ふ', 'nameEng' => 'Urban Prefecture');
      $g->region   = $r;
      $g->island   = $i;
      $g->capital  = $c;

      unset($g->name); unset($g->nameKana); unset($g->nameEng);
      unset($r->id); unset($i->id); unset($c->id);
      unset($g->region_id); unset($r->island_id); unset($c->prefecture_id);
    }

    array_unshift($get, array(
      'id' => 0,
      'capital' => array('name' => '', 'nameKana' => '', 'nameEng' => ''),
      'code'    => array('name' => '', 'nameKana' => '', 'nameEng' => ''),
      'pref'    => array('name' => '', 'nameKana' => '', 'nameEng' => ''),
      'type'    => array('name' => '', 'nameKana' => '', 'nameEng' => ''),
      'region'  => array('name' => '', 'nameKana' => '', 'nameEng' => ''),
      'island'  => array('name' => '', 'nameKana' => '', 'nameEng' => '')
    ));

    return $get;
  }

  public function getGeoIsland () {
    $get = DB::table('nhn_island')->get();
    return $get;
  }

  public function getGeoCity ($id) {
    $get = DB::table('nhn_city')->where('prefecture_id', $id)->get();

    foreach ($get as $g) {
      $p = DB::table('nhn_prefecture')->where('id', $g->prefecture_id)->first();
      $r = DB::table('nhn_region')->where('id',     $p->region_id)->first();
      $i = DB::table('nhn_island')->where('id',     $r->island_id)->first();

      $dc = DB::table('nhn_city_district')->where('city_id', $g->id)->count();

      if ($dc > 0) {
        $district = DB::table('nhn_city_district')->where('city_id', $g->id)->get();
        foreach ($district as $d) { unset($d->id); unset($d->city_id); }
        $g->district = $district;
      }

      $g->prefecture = $p;
      $g->region = $r;
      $g->island = $i;

      unset($p->id); unset($r->id); unset($i->id);
      unset($g->prefecture_id); unset($p->region_id); unset($r->island_id);
    }

    return $get;
  }

  public function getGeoDistrict ($id) {
    $get = DB::table('nhn_city_district')->where('city_id', $id)->get();
    foreach ($get as $g) { unset($g->city_id); }
    return $get;
  }

  public function getRailCompany () {
    $get = DB::table('nhn_company')->get();
    foreach ($get as $g) { unset($g->id); }

    return $get;
  }

  public function getRailLine () {
    $get = DB::table('nhn_line')->get();

    return $get;
  }

  public function getRailStation () {
    $get = DB::table('nhn_station')->get();

    return $get;
  }

  public function getHistoryPeriod () {
    $get = DB::table('nhn_jidai')->get();

    foreach ($get as $g) {
      $g->current = ($g->current == 1 ?? false);
      $g->years = ($g->years != 999 ?: '現在');
      $g->finish_date = ($g->finish_date != '2099-12-31' ?: '未定');

      unset($g->id);
    }

    return $get;
  }

  public function getHoliday ($year) {
    $get = DB::table('nhn_holiday')->where('holiday_date', 'LIKE', $year.'-%')->get();

    return $get;
  }
}
