<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Helpers;
use Auth;

class PageController extends Controller {
    /**
     * Display icons page
     *
     * @return \Illuminate\View\View
     */
    public function new_user() {
        return view('pages.new_user');
    }

    /**
     * Display maps page
     *
     * @return \Illuminate\View\View
     */
    public function maps() {
        return view('pages.maps');
    }

    /**
     * Display tables page
     *
     * @return \Illuminate\View\View
     */
    public function tables() {
        $user = Auth::user();
        if($user->hasRole('admin')) {
            //if admin, return view with corresponding data
            $id = Auth::id();

            // Needs to be modified for when a user switches between owned clubs. Currently getting first match.
            $club_info = DB::table('clubs')
                ->where('owner_id', $id)
                ->first();

            $club_id = $club_info->club_id;

            //get players and notifications for club
            $notifications = DB::table('stats_test')
                ->select('player_id','full_name', 'notification')
                ->where('club_id','=', $club_id)
                ->whereNotNull('notification')
                ->get();

            $owner_clubs = Helpers::findClubsByOwnerId(Auth::id());

            return view('pages.tutorial')
                ->with('owner_clubs', $owner_clubs)
                ->with('notifications', $notifications);
        } else {
            //if player, simply display the page
            return view('pages.tutorial');
        }
    }

    /**
     * Display notifications page
     *
     * @return \Illuminate\View\View
     */
    public function notifications() {
        return view('pages.notifications');
    }

    /**
     * Display rtl page
     *
     * @return \Illuminate\View\View
     */
    public function rtl() {
        return view('pages.rtl');
    }

    /**
     * Display typography page
     *
     * @return \Illuminate\View\View
     */
    public function typography() {
        return view('pages.typography');
    }

    /**
     * Display upgrade page
     *
     * @return \Illuminate\View\View
     */
    public function upgrade() {
        return view('pages.upgrade');
    }

    /**
     * Display leaderboard page
     *
     * @return \Illuminate\View\View
     */
    public function leaderboard() {
        $user = Auth::user();

        if ($user->hasRole('admin')) {
            $id = Auth::id();

            //Needs to be modified for when a user switches between owned clubs. Currently getting first match.
            $club_info = DB::table('clubs')
                ->where('owner_id', $id)
                ->first();

            $club_id = $club_info->club_id;

            $notifications = DB::table('stats_test')
                ->select('player_id', 'full_name', 'notification')
                ->where('club_id', '=', $club_id)
                ->whereNotNull('notification')
                ->get();

            $owner_clubs = Helpers::findClubsByOwnerId(Auth::id());

            $players = DB::table('players')
                ->select('player_id', 'first_name', 'last_name', 'rating')
                ->where('owner_id', $id)
                ->orderBy('rating', 'DESC')
                ->get();

            return view('pages.leaderboard')
                ->with('owner_clubs', $owner_clubs)
                ->with('notifications', $notifications)
                ->with('club_info', $club_info)
                ->with('players', $players);
        } else {
            $id = Auth::id();

            $player_email = DB::select("SELECT email FROM users WHERE id= '$id';");

            $player_email = $player_email[0]->email;

            $player_id = DB::select("SELECT player_id FROM players WHERE email= '$player_email';");

            $player_id = $player_id[0]->player_id;

            $owner_id = DB::select("SELECT owner_id FROM players WHERE player_id= '$player_id';");

            //This is an inefficient way to retrieve the owner_id when logged in as player. Probably better ways to do this.
            $owner_id = $owner_id[0]->owner_id;

            //Needs to be modified for when a user switches between owned clubs. Currently getting first match.
            $club_info = DB::table('clubs')
                ->where('owner_id', $owner_id)
                ->first();

            $club_id = $club_info->club_id;

            $players = DB::table('players')
                ->select('player_id', 'first_name', 'last_name', 'rating')
                ->where('owner_id', $owner_id)
                ->orderBy('rating', 'DESC')
                ->get();

            return view('pages.leaderboard')
                ->with('club_info', $club_info)
                ->with('players', $players);
        }
    }
}
