<?php

namespace App\Presenters;

use Laracasts\Presenter\Presenter;

/**
*
*@author : Tiamiyu waliu
*@website : http://www.iDocrea8.com
*/
class PostPresenter extends Presenter
{
    /**
     * Present text
     *
     * @return string
     */
    public function text($text = null)
    {
        $text = (empty($text)) ? $this->entity->text : $text;
        //$text = htmlspecialchars($text);
        $text = nl2br($text);

        //turn links to clickable
        $text = app('App\\Repositories\\PostRepository')->turnLinks($text);

        return \Hook::fire('post-text', $text);
    }

    public function hasTooMuchText()
    {
        return (strlen($this->entity->text) > \Config::get('post-text-max-show'));
    }

    public function hasOneImage()
    {

        $images = $this->images();
        return (count($images) > 1) ? false : true;
    }

    public function images()
    {

        $images = perfectUnserialize($this->entity->type_content);

        $photos = [];

        if (!$images) return $photos;
        foreach($images as $image) {
            $file = base_path(str_replace('%d', 600, $image));

            if(file_exists($file)) {
                $photos[] = $image;
            }
        }

        return $photos;
    }

    public function getVideoUrl()
    {
        $content = perfectUnserialize($this->entity->type_content);
        if (!empty($content)) {
            $link = $content[0];
            $youTube = $this->parseYouTube($link);
            if (!empty($youTube)) return $youTube;

            $vimeoURL = $this->parseVimeo($link);
            if (!empty($vimeoURL)) return $vimeoURL;
        }

        return false;
    }

    public function parseYouTube($link)
    {
        if(preg_match("#embed#", $link)) return $link;

        //this take care of youtube link like this http://youtu.be/awerqwouioqw
        if(preg_match('#http://#', $link))
        {
            if(preg_match('#http://youtu.be#', $link))
            {
                $link = str_replace('http://youtu.be', 'http://www.youtu.be', $link);

            }elseif( preg_match('#http://youtube.com#', $link))
            {
                $link = str_replace('http://youtube.com', 'http://www.youtube.com', $link);
            }

            if(preg_match("#http:\/\/youtu.be\/[a-zA-Z0-9\-\_\.]+#", $link, $matchs))
            {
                return 'http://www.youtube.com/embed/'.str_replace("http://youtu.be/",'', $link);
            }

            //this take care of youtube http://youtube.com/watch?v=dfkjsdfhsdjk
            if(preg_match("#http:\/\/www.youtube.com\/watch\?v\=[a-zA-Z0-9\-\_\.]+#", $link, $matchs))
            {
                return 'http://www.youtube.com/embed/'.str_replace("http://www.youtube.com/watch?v=",'', $link);
            }

        }elseif(preg_match('#https://#', $link))
        {
            if(preg_match('#https://youtu.be#', $link))
            {
                $link = str_replace('https://youtu.be', 'https://www.youtu.be', $link);

            }elseif( preg_match('#https://youtube.com#', $link))
            {
                $link = str_replace('https://youtube.com', 'https://www.youtube.com', $link);
            }

            if(preg_match("#https:\/\/www.youtu.be\/[a-zA-Z0-9\-\_\.]+#", $link, $matchs))
            {
                return 'https://www.youtube.com/embed/'.str_replace("https://www.youtu.be/",'', $link);
            }

            //this take care of youtube http://youtube.com/watch?v=dfkjsdfhsdjk
            if(preg_match("#https:\/\/www.youtube.com\/watch\?v\=[a-zA-Z0-9\-\_\.]+#", $link, $matchs))
            {

                return 'https://www.youtube.com/embed/'.str_replace("https://www.youtube.com/watch?v=",'', $link);
            }
        }

        return false;
    }

    public function parseVimeo($link)
    {
        $link = str_replace('http://www.','', $link);
        $link = str_replace('http://','', $link);
        $link = str_replace('https://','', $link);
        $link = str_replace('www.','', $link);
        //$link = str_replace('https://wwww.','', $link);

        //echo $link;
        if(preg_match('#vimeo.com\/[a-zA-Z0-9\-\_]#', $link))
        {
            //return true;
            $link = str_replace('vimeo.com/','vimeo.com/video/',$link);
            $link = 'http://player.'.$link;

            return $link;
        }
        return false;
    }

    public function hasLink()
    {
        $link = (empty($this->entity->link)) ? [] : perfectUnserialize($this->entity->link);

        if (empty($link) or empty($link['title'])) return false;

        return true;
    }

    public function getLinkDetail()
    {
        $link = (empty($this->entity->link)) ? [] : perfectUnserialize($this->entity->link);

        return $link;
    }

    public function getSoundCloudUrl()
    {
        $content = perfectUnserialize($this->entity->type_content);
        if (!empty($content) and !empty($content[0])) {
            return 'https://w.soundcloud.com/player/?url='.$content[0];
        }
        return false;
    }

    public function getLocation()
    {
        $content = perfectUnserialize($this->entity->type_content);
        if (!empty($content)) {
            return $content[0];
        }

        return false;
    }

    public function generalMediaValue()
    {
        $content = perfectUnserialize($this->entity->type_content);
        if (!empty($content)) {
            return $content[0];
        }

        return false;
    }

    public function time()
    {
        return str_replace(' ', 'T', $this->entity->created_at).'Z';
    }

    public function canReceiveNotification()
    {
        $check = app('App\\Repositories\\NotificationReceiverRepository')->exists(\Auth::user()->id, 'post', $this->entity->id);

        return ($check) ? 1 : 0;
    }

    public function canDelete()
    {
        if (!\Auth::check()) return false;
        if (\Auth::user()->id == $this->entity->user_id or \Auth::user()->isAdmin()) return true;

        return false;
    }

    public function getPrivacy()
    {
        switch($this->entity->privacy) {
            case 1:
                return trans('global.public');
            break;
            case 2:
                return trans('connection.friends');
            break;
            case 3:
                return trans('connection.followers');
            break;
            case 4:
                return trans('connection.friends-followers');
            break;
            default:
                return trans('user.only-me');
        }
    }

    public function listTags()
    {

        $tags = (empty($this->entity->tags)) ? [0] : perfectUnserialize($this->entity->tags);

        if (empty($tags)) return [];

        return app('App\\Repositories\\UserRepository')->findByIds($tags);
    }

    public function moreTags()
    {

        $limit = \Config::get('post-tags-member-limit');

        return $this->listLimitedTags($limit);
    }

    public function url()
    {
        return \URL::route('post-page', ['id' => $this->entity->id]);
    }

    public function isAutoPost()
    {
        return ($this->entity->content_type == 'auto-post');
    }

    public function getAutoPost()
    {
        return $content = perfectUnserialize($this->entity->type_content);
    }
}