<?php
namespace twa\htmlhelpers\helpers;

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

class structuredData
{

    protected $product;
    protected $store;

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

    public function generate($store = false,$product = false,$slug = false,$links = []){
        $data = '';
        if(ecom('url')->prefix() == '/'.request()->path()){
            $data .= '<script type="application/ld+json"> {
                    "@context": "https://schema.org",
                    "@type": "Organization",
                    "name": "'.env('APP_NAME').'",
                    "alternateName": "'.env('APP_NAME').', '.$store['label'].'",
                    "url": "'.ecom('url')->prefix(true).'",
                    "logo": "'.env('APP_URL').'/assets/images/logo.jpg",
                    "sameAs": [
                        '.collect($links)->map(function($values){ return '"'.$values.'"'; })->implode(',').'
                    ] }
                    </script>
                    <script type="application/ld+json"> {
                        "@context": "https://schema.org/",
                        "@type": "WebSite",
                        "name": "'.env('APP_NAME').'",
                        "url": "'.ecom('url')->prefix(true).'", "potentialAction": {
                        "@type": "SearchAction",
                        "target": "'.ecom('url')->prefix(true).'/search/?term={search_term_string}", "query-input": "required name=search_term_string"
                        } }
                        </script>';
        }


        if($product){
//            dd($product);
            $data .= '<script type="application/ld+json"> {
                "@context": "https://schema.org/",
                "@type": "Product",
                "name": "'.$product['label'].'",
                "image": "'.$product['thumb'].'", "description": "'.$product['small_description'].'",
                "sku": "'.$product['sku'].'",
                "mpn": "'.$product['sku'].'",
                "brand": { "@type": "Brand", "name": "'.env('APP_NAME').'"
                }, "offers": {
                "@type": "Offer",
                "url": "'.ecom('url')->prefix(true).'/product/'.$product['slug'].'", "priceCurrency": "'.$product['unit_price']['currency'].'",
                "price": "'.$product['unit_price']['price_not_formatted'].'"
                } }
                </script>';
        }


        if($slug){

            $data .= '<script type="application/ld+json"> {
                "@context": "https://schema.org/",
                "@type": "BreadcrumbList",
                "itemListElement": [';

            $i=0;
            foreach ($slug AS $rowSlug){
                $i++;
                $data .= '{
                    "@type": "ListItem",
                    "position": '.$i.',
                    "name": "'.addslashes($rowSlug['label']).'",
                    "item": "'.addslashes($rowSlug['link']).'"
                    }';
                if($i<count($rowSlug)){
                    $data .= ',';
                }
            }


            $data.= ']
                }</script>
                </script>';
        }



        echo $data;
    }

}
