<?php

namespace App\Controllers;

use App\Controllers\Base\ProfileBaseController;
use App\Interfaces\PhotoRepositoryInterface;
use App\Repositories\BlockedUserRepository;
use App\Repositories\PhotoRepository;
use App\Repositories\PostRepository;
use App\Repositories\UserRepository;

/**
*
*@author : Tiamiyu waliu
*@website : http://www.iDocrea8.com
*/
class ProfileController extends ProfileBaseController
{
    public function __construct(
        PostRepository $postRepository,
        BlockedUserRepository $blockedUserRepository,
        PhotoRepositoryInterface $photoRepositoryInterface,
        UserRepository $userRepository
    )
    {
        parent::__construct();
        $this->postRepository = $postRepository;
        $this->blockUserRepository = $blockedUserRepository;
        $this->photo = $photoRepositoryInterface;
        $this->userRepository = $userRepository;
    }
    public function index()
    {
        if (!$this->exists()) {
            return $this->profileNotFound();
        }

        if (\Auth::check() and $this->blockUserRepository->hasBlock(\Auth::user()->id, $this->profileUser->id)) {
            return \Redirect::route('user-home');
        }

        return $this->render('profile.index', [
            'posts' => $this->postRepository->lists('profile', 0, $this->profileUser->id)],
            [
                'title' => $this->setTitle()
            ]
        );
    }

    public function uploadCover()
    {
        $failed = json_encode([
            'status' => 'error',
            'message' => trans('photo.error', ['size' => formatBytes()])
        ]);
        if (!\Input::hasFile('img')) return $failed;

        $file = \Input::file('img');

        if (!$this->photo->imagesMetSizes($file)) return $failed;

        $path = $file->getRealPath();

        list($width, $height) = getimagesize($path);

        $result = json_encode([
            'status' => 'error',
            'message' => 'Insufficient image width/Height, MinWidth : 960px and MinHeight :  300px'
        ]);
        if ($width < 960 or $height < 300) {
            return $result;
        }
        if ($width < 1000) {
            //let use direct upload like that
            $image = $this->photo->exactUpload($file, 'temp/', 960, 300);

            if ($image->hasError()) return $result;

            $image = $image->result();
        } else {
            $image = $this->photo->upload($file, [
                'path' => 'temp/',
                'width' => 1000,
                'fit' => 'inside',
                'scale' => 'down'
            ]);

            if (!$image) return $result;
            $image = str_replace('_%d_', '_1000_', $image);
        }




        if ($image) {

            list($width, $height) = getimagesize(base_path().'/'.$image);
            return json_encode([
                'status' => 'success',
                'url' => \URL::to($image),

            ]);
        }

        return $result;

    }

    public function cropCover()
    {
        $top = \Input::get('imgY1');
        $left = \Input::get('imgX1');
        $cWidth = \Input::get('cropW');
        $cHeight = \Input::get('cropH');
        $file = \Input::get('imgUrl');
        $file = str_replace( [\URL::to(''), '//'],[ '', '/'], $file);

        $image = $this->photo->cropImage(base_path('').$file, 'cover/', $left, $top, $cWidth, $cHeight, false);
        $image = str_replace('%d', 'original', $image->result());

        /**make sure to delete the original image***/
        $this->photo->delete($file);

        if (!empty($image)) {
            /**
             * Update user profile cover
             */
            $this->userRepository->updateCover($image);
            return json_encode([
                'status' => 'success',
                'url' => \URL::to($image),
            ]);
        } else {
            return json_encode([
                'status' => 'error',
                'message' => 'Error things',
            ]);
        }


    }
}
