<?php

namespace App\Controller\Member;

use App\Controller\Member\AppMemberController;

class ToolsController extends AppMemberController
{
    public function quick()
    {
        $this->loadModel('Users');

        $user = $this->Users->find()->contain('Plans')->where(['Users.id' => $this->Auth->user('id')])->first();
        $this->set('user', $user);

        $notice = '';
        if (!$this->loggedin_user_plan->api_quick) {
            $notice = __('You must upgrade your plan so you can use this tool.');
        }
        $this->set('notice', $notice);
    }

    public function massShrinker()
    {
        $this->loadModel('Users');

        $user = $this->Users->find()->contain('Plans')->where(['Users.id' => $this->Auth->user('id')])->first();
        $this->set('user', $user);

        $notice = '';
        if (!$this->loggedin_user_plan->api_mass) {
            $notice = __('You must upgrade your plan so you can use this tool.');
        }
        $this->set('notice', $notice);

        $link = $this->Users->Links->newEntity();
        if ($this->request->is('post')) {
            if ($notice) {
                $this->Flash->error($notice);
                return $this->redirect(['action' => 'massShrinker']);
            }

            $urls = explode("\n", str_replace("\r", "\n", $this->request->data['urls']));
            $urls = array_unique(array_filter($urls));
            $urls = array_slice($urls, 0, get_option('mass_shrinker_limit', 20));
            $urls = array_map('trim', $urls);

            $type = get_option('member_default_redirect', 1);
            if (isset($this->request->data['type'])) {
                if (array_key_exists($this->request->data['type'], get_allowed_redirects())) {
                    $type = $this->request->data['type'];
                }
            }

            $results = [];
            foreach ($urls as $url) {
                $results[] = $this->addMassShrinker($url, $type);
            }

            $this->set('results', $results);
        }
        $this->set('link', $link);
    }

    public function api()
    {
        $this->loadModel('Users');

        $user = $this->Users->find()->contain('Plans')->where(['Users.id' => $this->Auth->user('id')])->first();
        $this->set('user', $user);

        $notice = '';
        if (!$this->loggedin_user_plan->api_developer) {
            $notice = __('You must upgrade your plan so you can use this tool.');
        }
        $this->set('notice', $notice);
    }

    public function full()
    {
        $this->loadModel('Users');

        $user = $this->Users->find()->contain('Plans')->where(['Users.id' => $this->Auth->user('id')])->first();
        $this->set('user', $user);

        $notice = '';
        if (!$this->loggedin_user_plan->api_full) {
            $notice = __('You must upgrade your plan so you can use this tool.');
        }
        $this->set('notice', $notice);
    }

    public function bookmarklet()
    {
        $this->loadModel('Users');

        $user = $this->Users->find()->contain('Plans')->where(['Users.id' => $this->Auth->user('id')])->first();
        $this->set('user', $user);

        $notice = '';
        if (!$this->loggedin_user_plan->bookmarklet) {
            $notice = __('You must upgrade your plan so you can use this tool.');
        }
        $this->set('notice', $notice);
    }


    protected function addMassShrinker($url, $type = 1)
    {
        $this->loadModel('Links');

        $result = ['url' => '', 'short' => ''];

        $url = parse_url($url, PHP_URL_SCHEME) === null ? 'http://' . $url : $url;

        $link = $this->Links->find()->where(['url' => $url, 'user_id' => $this->Auth->user('id')])->first();

        if ($link) {
            return ['url' => $url, 'short' => $link->alias];
        }

        $link = $this->Links->newEntity();
        $data = [];

        $data['user_id'] = $this->Auth->user('id');
        $data['url'] = $url;
        $data['alias'] = $this->Links->geturl();
        $data['type'] = $type;
        $link->status = 1;
        $link->clicks = 0;
        $link->method = 4;

        $linkMeta = [
            'title' => '',
            'description' => '',
            'image' => ''
        ];

        if (get_option('disable_meta_api') === 'no') {
            $linkMeta = $this->Links->getLinkMeta($url);
        }

        $data['title'] = $linkMeta['title'];
        $data['description'] = $linkMeta['description'];
        $link->image = $linkMeta['image'];

        $link = $this->Links->patchEntity($link, $data);
        if ($this->Links->save($link)) {
            return ['url' => $url, 'short' => $link->alias, 'domain' => $link->domain];
        }
        return ['url' => $url, 'short' => 'error', 'domain' => ''];
    }
}
