<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FavoriteController extends Controller
{
    public function index(){
		
        $css = root("css/favorite/index.css");
        $bread = true;
        $staticFirst =array(
            'url' => root('img/favorite/top.jpg'),
            'title' => "FAVORITE",
            'jp' => 'お気に入り'
        );
        return view('home.favorite.index', compact('css','bread','staticFirst'));
    }
	
	
	/**
	* ------ TODO ---------
	* Update favorite - Add or Remove
	*
	**/
	public function update(Request $request, $id, $add = 1){
		$num = $request->session()->get('num_favourite', 0);
		$favourites = $request->session()->get('favourites', []);
	
		$num = $num + 1;
		if (!in_array($id, $favourites)){
			$favourites[] = $id;
		}
		else {
			if (($key = array_search($id, $favourites)) !== false) {
				unset($favourites[$key]);
			}
		}
		$request->session()->put('num_favourite', $num);
		$request->session()->put('favourites', $favourites);
		return count($favourites);
	}
	
}
