<?php namespace SureSoftware\PowerSEO\Components;

use Cms\Classes\ComponentBase;
use Illuminate\Support\Facades\App;
use RainLab\Pages\Classes\Router;
use Cms\Classes\Theme;
use SureSoftware\PowerSEO\models\Settings;
use Request;

class StaticPage extends ComponentBase
{
    public $page;
    public $seo_title;
    public $seo_description;
    public $seo_keywords;
    public $canonical_url;
    public $redirect_url;
    public $robot_index;
    public $robot_follow;
    public $title;

    public $ogTitle;
    public $ogUrl;
    public $ogDescription;
    public $ogSiteName;
    public $ogFbAppId;
    public $ogLocale;
    public $ogImage;


    public function componentDetails()
    {
        return [
            'name' => 'suresoftware.powerseo::lang.component.static.name',
            'description' => 'suresoftware.powerseo::lang.component.static.description'
        ];
    }

    public function defineProperties()
    {
        return [];
    }

    public function onRun()
    {
        $url = Request::path();

        // Remove language prefix in case it exists (e.g. from "/en/my-page" to "/my-page")
        if (class_exists('RainLab\Translate\Behaviors\TranslatableModel')) {
//            $url = preg_replace('/\/[a-z]{2}\//', '/', $url);
            $url = substr($url,2);
//            dd($url);
        }

        if (!strlen($url)) {
            $url = '/';
        }
        $router = new Router(Theme::getActiveTheme());
        $this->page = $this->page['page'] = $router->findByUrl($url);

        if ($this->page) {
            $this->seo_title = $this->page['seo_title'] = $this->page->viewBag['meta_title'] ?? '';
            $this->title = $this->page['title'] = $this->page->viewBag['title'];
            $this->seo_description = $this->page['seo_description'] = $this->page->viewBag['meta_description'] ?? '';
            $this->seo_keywords = $this->page['seo_keywords'] = $this->page->viewBag['meta_keywords'] ?? '';
//            $this->canonical_url = $this->page['canonical_url'] = $this->page->getViewBag()->property('canonical_url');
//            $this->redirect_url = $this->page['redirect_url'] = $this->page->getViewBag()->property('redirect_url');
            $this->robot_index = $this->page['robot_index'] = $this->page->getViewBag()->property('robot_index');
            $this->robot_follow = $this->page['robot_follow'] = $this->page->getViewBag()->property('robot_follow');

            $settings = Settings::instance();

            if ($settings->enable_og_tags) {
                $this->ogTitle = empty($this->page->meta_title) ? $this->page->title : $this->page->meta_title;
                $this->ogDescription = $this->page->meta_description;
                $this->ogUrl = empty($this->page->canonical_url) ? Request::url() : $this->page->canonical_url;
                $this->ogSiteName = $settings->og_sitename;
                $this->ogFbAppId = $settings->og_fb_appid;
            }
        }

    }

}
