<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Requests\SongDemand;
use App\Song ;
use App\Artist ;
use App\Demand ;
use App\SiteSetting ;

class SongController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth')->except('search','show','create','store','edit','mostVisited') ;
    }

    public function index()
    {
        //
    }

    public function search(Request $request)
    {
        $q = $request->input('q', '') ;
        $songs   = Song::search($q)->orderBy('title');
        $artists = Artist::search($q)->orderBy('name');

        if ($request->has('w')) {
            $type = $request->input('w') ;
            $flag = false;
            switch ($type) {
                case 'songs':
                $with = $songs->paginate(10) ;
                if ($with->count() > 0) $flag = true;
                break;

                case 'artists':
                $with = $artists->paginate(10) ;
                if ($with->count() > 0) $flag = true;
                break;

                default:
                $with = collect();
                break;
            }
            return view('songs.withresult', compact('with', 'type', 'q', 'flag')) ;
        }

        $songs   = $songs->paginate(5) ;
        $artists = $artists->paginate(5) ;

        return view('songs.result', compact('artists', 'songs', 'q')) ;
    }

    public function create()
    {
        if (SiteSetting::get('submit_lyrics_enabled', '1') == '0' && (!Auth::check() || Auth::user()->role != 'admin')) {
            return redirect('/');
        }
        return view('songs.create') ;
    }

    public function store(SongDemand $request)
    {
        if (SiteSetting::get('submit_lyrics_enabled', '1') == '0' && (!Auth::check() || Auth::user()->role != 'admin')) {
            return redirect('/');
        }

        // reCAPTCHA verification
        if (SiteSetting::get('recaptcha_enabled', '0') == '1' && SiteSetting::get('recaptcha_secret_key', '')) {
            $recaptcha = $request->input('g-recaptcha-response');
            if (!$recaptcha) {
                return back()->withInput()->withErrors(['g-recaptcha-response' => 'يرجى تأكيد أنك لست برنامج روبوت']);
            }
            $response = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . SiteSetting::get('recaptcha_secret_key') . '&response=' . $recaptcha);
            $result = json_decode($response);
            if (!$result || !$result->success) {
                return back()->withInput()->withErrors(['g-recaptcha-response' => 'فشل التحقق من reCAPTCHA، يرجى المحاولة مرة أخرى']);
            }
        }
        $demand          = new Demand ;
        $demand->song    = $request->input('song') ;
        $demand->artist  = $request->input('artist') ;
        $demand->lyrics  = $request->input('lyrics') ;
        $demand->type    = $request->input('type') ;
        $demand->user_id = Auth::id() ;
        $demand->save() ;
        flash('شكراً لك على إرسال الكلمات')->warning() ;
        return redirect()->home() ;
    }

    public function show(Song $song)
    {
        $song->increment('views');
        return view('songs.show', compact('song')) ;
    }

    public function mostVisited()
    {
        $songs = Song::with('artists')->orderBy('views', 'desc')->get();
        return view('songs.most-visited', compact('songs'));
    }

    public function edit(Song $song)
    {
        if (SiteSetting::get('correction_enabled', '1') == '0' && (!Auth::check() || Auth::user()->role != 'admin')) {
            return redirect('/');
        }
        return view('songs.edit', compact('song')) ;
    }

    public function update(Request $request, $id)
    {
        //
    }

    public function destroy($id)
    {
        
    }
}
