<?php

namespace App\Http\Controllers;

use App\Gallery;
use App\Album;
use Illuminate\Http\Request;
use Response;
use Storage;
use App\Http\Services\Uploader; #custom class

class GalleryController extends Controller
{
  /**
   * Display a listing of the resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function index()
  {
      //
  }

  /**
   * Show the form for creating a new resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function create()
  {
    
  }

  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $request)
  {

    $order = Album::find($request->album)->galleries()->count();
    $db = new Gallery();

    # If new image uploaded
    if($request->image) {
      
      # Save new image
      $path = Uploader::SaveGallery($request->image, $request->subfolder, 'img/gallery/');

    }

    $db->path = $path;
    $db->album_id = $request->album;
    $db->order = $order+1;
    $db->save();

    $album = Album::find($request->album);
    
    $list = '<li class="gallery-item" s-val="'.$db->id.'">
              <div class="item-wrapper">
                <div class="image">
                  <img src="'.asset(config("customvar.storage_url")."img/gallery/".$album->slug."/".str_replace(".", "_tn.", $db->path)).'" alt="">
                </div>
                <div class="caption">
                  <h5> No Caption </h5>
                </div>
                <div class="controls">
                  <a s-id="'.$db->id.'" class="btn btn-default btn-xs btn-edit">
                    <i class="fa fa-pencil"></i>
                  </a>
                  <a s-id="'.$db->id.'" class="btn btn-danger btn-xs btn-delete">
                    <i class="fa fa-trash"></i>
                  </a>
                </div>
              </div>
            </li>';
    return Response::json(['message' => 'Successfully uploaded.', 'list' => $list]);
  }

  /**
   * Display the specified resource.
   *
   * @param  \App\Gallery  $gallery
   * @return \Illuminate\Http\Response
   */
  public function show(Gallery $gallery)
  {
      //
  }

  /**
   * Show the form for editing the specified resource.
   *
   * @param  \App\Gallery  $gallery
   * @return \Illuminate\Http\Response
   */
  public function edit(Gallery $gallery)
  {
    $data = collect($gallery);

    $translations = collect([]);

    foreach (config('translatable.locales') as $key => $value) {
      $tranlation = $gallery->hasTranslation($key) ? $gallery->translate($key) : '';
      $translations->put($key, $tranlation);
    }

    $data->put('translations', $translations);

    return Response::json($data);
  }

  /**
   * Update the specified resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \App\Gallery  $gallery
   * @return \Illuminate\Http\Response
   */
  public function update(Request $request, Gallery $gallery)
  {

    foreach (config('translatable.locales') as $key => $value) {
      $gallery->{'caption:'.$key} = $request->caption[$key];
      $gallery->{'alt:'.$key} = $request->alt[$key];
    }

    // return $data;
    $gallery->save();

    return Response::json('Successfully updated');

  }

  /**
   * Remove the specified resource from storage.
   *
   * @param  \App\Gallery  $gallery
   * @return \Illuminate\Http\Response
   */
  public function destroy(Gallery $gallery)
  {

      # Get the old image name 
      $imageName = $gallery->path;

      # Delete old image if new one uploaded
      if($imageName!=''){
        Storage::delete("img/gallery/".$gallery->album->slug."/".str_replace(".", "_tn.", $gallery->path));
        Storage::delete("img/gallery/".$gallery->album->slug."/".$gallery->path);   
      }
      $gallery->delete();

      return Response::json('Successfully deleted');
  }

  /**========== Added functions =========**/

  /**========== Reorder =========**/
  public function reorder(Request $request)
  {
      $order = 1;
      foreach ($request->list as $key) {
          $db = Gallery::updateOrCreate(
              ['id' => $key], ['order' => $order]
          );
          $order++;
      }
      return Response::json('Successfully rearranged');

  }
  /**========== /. Reorder =========**/
}
