<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Media;
use View,Session,DB;
use Illuminate\Http\Request;
use Illuminate\Http\Input;
use Illuminate\Cookie\CookieJar;
use Symfony\Component\HttpFoundation\Cookie;
use Illuminate\Routing\Controller as BaseController;

class HomeController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/

	// ********** Main Home Controller //

	public function home($page = 0){

		if(\Input::get('theme') == 'dark'):
			return \Redirect::to('/')->withCookie('theme', 'dark');
		endif;

		if(\Input::get('theme') == 'clear'):
			$cookie = \Cookie::forget('theme');
			return \Redirect::to('/')->withCookie($cookie);
		endif;

		$images = DB::table('media')->orderBy('created_at', 'DESC')->offset($page*12)->take(12)->get();
		$settings = DB::table('settings')->first();
		return view('home.homepage', ['images'=>$images, 'settings'=>$settings]);
	}
	public function loadmore($pageNumber){
		$page = config('view.numPage');
		$pageNumber = $pageNumber;
		$offset = ($pageNumber - 1) * $page;
		$imagesLoad = DB::table('media')->offset($offset)->take($page)->orderBy('created_at', 'DESC')->get();
		return view('partials.loadmore', ['imagesLoad'=>$imagesLoad]);
	}

	public function like(Request $request, $id){
		if(isset($_POST['id'])){
			$id = intval($_POST['id']);
			$image = Media::where("id", "=", $id)->first();

			if ($request->session()->has('like')) {
				$arrLikes = $request->session()->get('like');
				if (in_array($id,$arrLikes)) {
					$image->count_like = $image->count_like - 1;
					$arrLikes = array_diff($arrLikes, [$id]);
					$request->session()->forget('like');
					$request->session()->put('like', $arrLikes);
				} else {
					$image->count_like = $image->count_like + 1;
					array_push($arrLikes,$id);
					$request->session()->forget('like');
					$request->session()->put('like', $arrLikes);
				}
			} else {
				$image->count_like = $image->count_like + 1;
				$arrLikes = [$id];		
				$request->session()->forget('like');
				$request->session()->put('like', $arrLikes);
			}
			$image->save();
			echo $image->count_like;
		}
	}
	public function seacrh(Request $request){
		$name = $request->input('search');
		$search =Media::where('title', 'LIKE', '%'.$name.'%')->get();
		return view('home.search', ['search'=>$search, 'name'=>$name]);
	}
	public function rss(){
		$rss = Media::orderBy('time_download', 'DESC')->take(20)->get();
		return view('home.rss', ['rss'=>$rss]);
	}
	public function popular(){
		$popular  = DB::table('media')->orderBy('count_download', 'DESC')->orderBy('time_download','DESC')->take(99)->get();
		return view('home.popular', ['popular'=>$popular]);
	}
}