<?php

namespace App\Http\Controllers\Video;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Engine;
// use Illuminate\Support\Facades\Log;

class VideoTable extends Engine {
  private $engine;

  public function __construct () {
    $this->engine = new Engine;
  }

  public function index ($slug) {
    $slg = DB::table('vid_game')->select('id', 'name')->where('slug', $slug)->first();
    if (!$slg) return view('pages.site.notfound');
    $res = DB::table('vid_video')->where('game_id', $slg->id)->orderBy('publish_date', 'desc')->get();
    if (!$res) return view('pages.site.notfound');

    foreach ($res as $r) {
      $r->publish_date = date('Y月m月d日', $r->publish_date);
      $r->gametitle = explode('】', $r->title);
      $r->title = (isset($r->gametitle[1]) ? $r->gametitle[1] : '');
      $r->gametitle = $r->gametitle[0];
      $r->gametitle = str_replace('【'.$slg->name, '', $r->gametitle);

      if ($r->gametitle == '') $r->gametitle = '初代';
    }

    return view('pages.site.video.videotable', ['res' => $res]);
  }

  public function add (Request $r) {
    if ($this->engine->user && $this->engine->user->perm['blg_addpost']) {
      $game = DB::table('vid_game')->get();
      $res = '';
      $frm = array('title' => '', 'vid' => '', 'game_id' => '', 'thumbnail' => '', 'url' => '', 'odysee' => '', 'kerotube' => '');

      if (isset($r->submit)) {
        if (empty($this->engine->err)) { if ($r->url == '' && $r->odysee == '' && $r->kerotube == '') $this->engine->err = '１以上の動画URLをご入力下さい。'; }

        // フォームの値を保存して
        $frm['title'] = $r->title;
        $frm['vid'] = $r->vid;
        $frm['game_id'] = $r->game_id;
        $frm['thumbnail'] = ($r->thumbnail ?: '');
        $frm['publish_date'] = time();
        $frm['url'] = ($r->url ?: '');
        $frm['odysee'] = ($r->odysee ?: '');
        $frm['kerotube'] = ($r->kerotube ?: '');

        // 件名、文章又はスラッグがなければ、エラーを出て
        $verify = array('件名' => $r->title, 'スラッグ' => $r->vid);
        $incomplete = array();
        if (empty($this->engine->err)) { 
          foreach ($verify as $k => $v) { if (is_null($v) || empty($v) || !isset($v)) $incomplete[] = $k; }
          if (count($incomplete) > 0) $this->engine->err = implode('、', $incomplete).'をご入力下さい。';
        }

        if (empty($this->engine->err)) { if ($r->game_id == 0) $this->engine->err = 'ゲームをご選択下さい。'; }

        // スラッグが既に存在したら、エラーを出て
        if (empty($this->engine->err)) { 
          $sl = DB::table('vid_video')->select('vid')->where('vid', $r->vid)->first();
          if ($sl && $sl->vid == $r->vid) $this->engine->err = 'このスラッグがもう存在しています。';
        }

        // エラーがあったら、フォームに戻って。なければ、データベースに保存したり、ページ又はポストへ移転して
        if (!empty($this->engine->err)) return view('pages.site.video.play.add', ['res' => $res, 'err' => $this->engine->err, 'frm' => $frm, 'game' => $game]);
        else {
          // できたの？
          if ($res = DB::table('vid_video')->insert($frm)) return redirect('/video/play/'.$r->vid);
          else {
            // やれやれ…
            return view('pages.site.video.play.add', ['res' => $res, 'err' => $this->engine->err, 'frm' => $frm, 'game' => $game]);
          }
        }
      }

      return view('pages.site.video.play.add', ['res' => $res, 'err' => $this->engine->err, 'frm' => $frm, 'game' => $game]);
    }

    return view('pages.site.notallowed');
  }

  public function delete (Request $r) {
    if ($this->engine->user->perm['blg_delpost']) {
      $get = DB::table('vid_video')->select('game_id')->where('vid', $r->slug)->first();
      $red = DB::table('vid_game')->select('slug')->where('id', $get->game_id)->first();
      DB::table('vid_video')->where('vid', $r->slug)->delete();
      return $red->slug;
    }

    return '許可がありません。';
  }

  public function edit (Request $r) {
    if ($this->engine->user && $this->engine->user->perm['blg_addpost']) {
      $game = DB::table('vid_game')->get();
      $frm = DB::table('vid_video')->where('vid', $r->vid)->first();
      if (!$frm) return view('pages.site.notfound');
      $res = '';

      if (isset($r->submit)) {
        if (empty($this->engine->err)) { if ($r->url == '' && $r->odysee == '' && $r->kerotube) $this->engine->err = '１以上の動画URLをご入力下さい。'; }

        // フォームの値を保存して
        $frm->title = $r->title;
        $frm->vid = $r->vid;
        $frm->game_id = $r->game_id;
        $frm->thumbnail = $r->thumbnail;
        $frm->url = ($r->url ?: '');
        $frm->odysee = ($r->odysee ?: '');
        $frm->kerotube = ($r->kerotube ?: '');

        // 件名、文章又はスラッグがなければ、エラーを出て
        $verify = array('件名' => $r->title, 'スラッグ' => $r->vid);
        $incomplete = array();
        if (empty($this->engine->err)) {
          foreach ($verify as $k => $v) { if (is_null($v) || empty($v) || !isset($v)) $incomplete[] = $k; }
          if (count($incomplete) > 0) $this->engine->err = implode('、', $incomplete).'をご入力下さい。';
        }

        if (empty($this->engine->err)) { if ($r->game_id == 0) $this->engine->err = 'ゲームをご選択下さい。'; }

        // エラーがあったら、フォームに戻って。なければ、データベースに保存したり、ページ又はポストへ移転して
        if (!empty($this->engine->err)) return view('pages.site.video.play.edit', ['res' => $res, 'err' => $this->engine->err, 'frm' => $frm, 'game' => $game]);
        else {
          // できたの？
          if ($res = DB::table('vid_video')->where('vid', $r->vid)->update((array)$frm)) return redirect('/video/play/'.$r->vid);
          else {
            // やれやれ…
            return view('pages.site.video.play.edit', ['res' => $res, 'err' => $this->engine->err, 'frm' => $frm, 'game' => $game]);
          }
        }
      }

      return view('pages.site.video.play.edit', ['res' => $res, 'err' => $this->engine->err, 'frm' => $frm, 'game' => $game]);
    }

    return view('pages.site.notallowed');
  }
}
