<?php
namespace Site\Controller;

use Site\Controller\Mods;
use Site\Lib\Auth;
use Site\Lib\Template;

class User {
  use Mods;

  public function login(array $params): void {
    if (!AUTH_ENABLED) return;
    try {
      $auth = new Auth;
      $user = $auth->getLoggedInUser();
      if ($user) {
        header('Location: /');
        exit();
      }

      $doLogin = $_SERVER['REQUEST_METHOD'] === 'POST';
      $error = '';
      $nyuU = '';

      if ($doLogin) {
        if (!\verify_csrf_token($_POST['csrf_token'])) {
          header('Location: /');
          exit();
        }
        unset($_POST['csrf_token']);

        $a = [];
        $isBot = false;
        if (count($_POST) === 4) {
          $i = 0;
          foreach ($_POST as $p) {
            $a[(int)$i] = $p;
            if ($i >= 2 && $p != '') $isBot = true;
            $i++;
          }
        }

        if ($isBot) {
          header('Location: /');
          exit();
        }

        $auth = new Auth($a[0]);
        $res = $auth->isUserExist($a[0]);
        if (!$res->isSuccess) {
          $error = $res->message;
        } else {
          $result = $auth->setToken($a[0], $a[1]);
          if (!$result->isSuccess) {
            $nyuU = $a[0];
            $error = $result->message;
          } else {
            header('Location: /');
            exit();
          }
        }
      }
      $tmpl = new Template('/');
      $pagetit = 'サインイン';
      $description = 'サイトにサインイン';

      $tmpl->assign('pagetit', $pagetit);
      $tmpl->assign('curPage', 'auth');
      $tmpl->assign('custCss', false);
      $tmpl->assign('menu', $this->getMenu());
      $tmpl->assign('description', $description);
      $tmpl->assign('error', $error);
      $tmpl->assign('nyuU', $nyuU);

      $tmpl->render('login');
    } catch (\Exception $e) {
      throw new \Exception($e->getMessage());
    }
  }

  public function logout(array $params): void {
    if (!AUTH_ENABLED) return;
    try {
      $auth = new Auth;
      $user = $auth->getLoggedInUser();
      if (!$user) {
        header('Location: /');
        exit();
      }

      $auth->logout();
      header('Location: /');
      exit();
    } catch (\Exception $e) {
      throw new \Exception($e->getMessage());
    }
  }

  public function register(array $params): void {
    if (!AUTH_REGISTER_ENABLED) return;
    try {
      $auth = new Auth;
      $user = $auth->getLoggedInUser();
      if ($user) {
        header('Location: /');
        exit();
      }

      $doRegister = $_SERVER['REQUEST_METHOD'] === 'POST';
      $error = '';
      $nyuU = '';
      $nyuE = '';

      if ($doRegister) {
        if (!\verify_csrf_token($_POST['csrf_token'])) {
          header('Location: /');
          exit();
        }
        unset($_POST['csrf_token']);

        $a = [];
        $isBot = false;
        if (count($_POST) === 8) {
          $i = 0;
          foreach ($_POST as $p) {
            $a[(int)$i] = $p;
            if ($i >= 4 && $p != '') $isBot = true;
            $i++;
          }
        }

        if ($isBot) {
          header('Location: /');
          exit();
        }

        $auth = new Auth;
        $res = $auth->mkUser($a[0], $a[1], $a[2], $a[3]);
        if (!$res->isSuccess) {
          $error = $res->message;
          $nyuU = $a[0];
          $nyuE = $a[3];
        } else {
          $auth = new Auth($a[0]);
          $auth->setToken($a[0], $a[1]);
          header('Location: /');
          exit();
        }
      }

      $tmpl = new Template('/');
      $pagetit = '登録';
      $description = 'サイトに登録';

      $tmpl->assign('pagetit', $pagetit);
      $tmpl->assign('curPage', 'auth');
      $tmpl->assign('custCss', false);
      $tmpl->assign('menu', $this->getMenu());
      $tmpl->assign('description', $description);
      $tmpl->assign('error', $error);
      $tmpl->assign('nyuU', $nyuU);
      $tmpl->assign('nyuE', $nyuE);

      $tmpl->render('register');
    } catch (\Exception $e) {
      throw new \Exception($e->getMessage());
    }
  }

  public function profile(array $params): void {
    if (!AUTH_ENABLED) return;
    $name = '';
    if (isset($params['name'])) $name = $params['name'];
    if ($name === '') {
      header('Location: /');
      exit();
    }

    try {
      $auth = new Auth;
      $user = $auth->getLoggedInUser();
      $u = $auth->getUser($name);
      if (!$u) {
        header('Location: /404');
        exit();
      }

      $tmpl = new Template('/');
      $suffix = $u->gender === '男' ? 'くん' : ($u->gender === '女' ? 'ちゃん' : 'さん');
      $pagetit = $u->altName.$suffix.'のプロフィール';
      $description = '';

      $tmpl->assign('pagetit', $pagetit);
      $tmpl->assign('curPage', 'auth');
      $tmpl->assign('custCss', false);
      $tmpl->assign('menu', $this->getMenu());
      $tmpl->assign('description', $description);
      $tmpl->assign('user', $user);
      $tmpl->assign('u', $u);

      $tmpl->render('profile');
    } catch (\Exception $e) {
      throw new \Exception($e->getMessage());
    }
  }
}
