<?php
namespace twa\htmlhelpers\helpers;

use twa\ecomgeneral\models\EcomStoreModel;
use twa\ecomprofile\models\EcomCountriesModel;

class seoHelper
{

    protected $cms_attribues;
    protected $fallback_title = NULL;
    protected $fallback_description = NULL;
    protected $fallback_keywords = NULL;
    protected $inherited = NULL;

    function __construct(){
        if(isset(request()->seo)){
            $this->cms_attribues = json_decode(request()->seo,1);
        }
    }

    public function setAttributes($cms_attribues){
        $this->cms_attribues = json_decode($cms_attribues,1);
        return $this;
    }

    public function setBladeInheritedTags($inherited){
        $this->inherited = $inherited;
        return $this;
    }

    public function setFallbackTitle($fallback_title){
        $this->fallback_title = $fallback_title;
        return $this;
    }

    public function setFallbackDescription($fallback_description){
        $this->fallback_description = $fallback_description;
        return $this;
    }

    public function setFallbackKeywords($fallback_keywords){
        $this->fallback_keywords = $fallback_keywords;
        return $this;
    }

    public function generate(){


        if(isset($this->cms_attribues[app()->getLocale()])){
            $info = $this->cms_attribues[app()->getLocale()];
        }else{
            $info = [ "title" => false, "keywords" => false, "description" => false ];
            if($this->inherited){
                echo $this->inherited; return false;
            }
        }



        if(isset($info["title"]) && $info["title"] != ""){ $title = $info["title"]; }else{ $title = $this->fallback_title; }
        if(isset($info["keywords"]) && $info["keywords"] != ""){ $keywords = $info["keywords"]; }else{ $keywords = $this->fallback_keywords; }
        if(isset($info["description"]) && $info["description"] != ""){ $description = $info["description"]; }else{ $description = $this->fallback_description; }

        if(isset(request()->seo_settings)){
            if((!$title || $title=="") && isset(request()->seo_settings['title_'.app()->getLocale()])){
                $title = request()->seo_settings['title_'.app()->getLocale()] ;
            }

            if(isset(request()->seo_settings['append_'.app()->getLocale()])){
                $title .= ' '.request()->seo_settings['append_'.app()->getLocale()];
            }

            if((!$keywords || $keywords=="") && isset(request()->seo_settings['keywords_'.app()->getLocale()])){
                $keywords = request()->seo_settings['keywords_'.app()->getLocale()] ;
            }

            if((!$description || $description=="") && isset(request()->seo_settings['description_'.app()->getLocale()])){
                $description = request()->seo_settings['description_'.app()->getLocale()] ;
            }
        }



        $return = '<title>'.$title.'</title>';
        $return .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
        $return .= '<meta name=viewport content="width=device-width, initial-scale=1">';
        $return .= '<meta name="robots" content="index, follow" />';
        if(isset(request()->store['countries'])){
            foreach (request()->store['countries'] AS $country){
                $hreflang = strtolower(app()->getLocale().'-'.$country['code']);
                $return .= '<link rel="alternate" href="'.url()->current().'"  hreflang="'.strtolower($hreflang).'"/>';
            }
        }

        $return .= '<link rel="canonical" href="'.url()->current().'"/>';
        if($description){ $return .= '<meta name="description" content="'.$description.'">'; }
        if($keywords){ $return .= '<meta name="keywrods" content="'.$keywords.'">'; }

        echo $return;
    }

}
