<?php

namespace App;

use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Database\Eloquent\Model;

class Search extends Model
{
    public static function getResults(){



        try{

            $term = Input::get('q', '');

            $basesql = config('queries.defaultProductsBaseSql');
            $productsPerPage = config('queries.productsPerPage');

            $sql = $basesql.' WHERE p.prod_status = 1';

            $sql  = $sql.' AND (
					p.prod_name RLIKE :keyword1
					OR b.brand_name RLIKE :keyword2
					OR ct.cat_name RLIKE :keyword3
					
					OR CONCAT(b.brand_name, \' \', p.prod_name) RLIKE :keyword4
					OR CONCAT(ct.cat_name, \' \', b.brand_name) RLIKE :keyword5
					OR CONCAT(p.prod_name, \' \', b.brand_name ) RLIKE :keyword6
					OR CONCAT(b.brand_name, \' \', ct.cat_name) RLIKE :keyword7
					)					
					ORDER BY p.prod_price ASC ';

            $keyword = self::strToRegExp($term);

            $pars = [
                ':keyword1' => $keyword,
                ':keyword2' => $keyword,
                ':keyword3' => $keyword,
                ':keyword4' => $keyword,
                ':keyword5' => $keyword,
                ':keyword6' => $keyword,
                ':keyword7' => $keyword,
                ':lang' => app()->getLocale()
            ];

            $products = DB::select($sql,$pars);


            $path = 'search';


            $products = self::arrayPaginator($products,$path);

            return $products;

        } catch (\Illuminate\Database\QueryException $e) {
            echo handle_error($e);
        } catch (\Exception $e) {
            echo handle_error($e);
        }

    }


    // Helper method


    private static function arrayPaginator($array,$path)
    {
        $request = request();
        $page = Input::get('page', 1);
        $perPage = config('queries.productsPerPage');
        $offset = ($page * $perPage) - $perPage;

        return new LengthAwarePaginator(array_slice($array, $offset, $perPage, true), count($array), $perPage, $page,
            ['path' => $path, 'query' => $request->query()]);
    }


    private static function strToRegExp($keyw) {

        $ukupnopoklapanja = strlen($keyw);
        $zazamenupret = array('c', 'č', 'ć', 'z', 'ž', 's', 'š', 'd', 'đ', ' ', '-', '+');
        $zamenapret0 = array('F1F1F1', 'F2F2F2', 'F3F3F3', 'F4F4F4', 'F5F5F5', 'F6F6F6', 'F7F7F7', 'F8F8F8', 'F9F9F9', 'F0F0F0', '~~~', '@@@@@' );
        $zamenapret = array("+[čcć]+", "+[čcć]+", "+[čcć]+", "+[zž]+", "+[zž]+", "+[sš]+", "+[sš]+", "+[đd]+", "+[đd]+", "+([ \t\-]{0,1})+", "+[-s]+", "+[+\s]+");
        $zamenjenopret0 = str_replace($zazamenupret, $zamenapret0, $keyw);
        $zamenjenopret = str_replace($zamenapret0, $zamenapret, $zamenjenopret0);

        $pozzadslov = (strlen($zamenjenopret)-1);
        if($zamenjenopret{$pozzadslov} == '+') { $rezultat1 = substr($zamenjenopret, 0, -1);} else { $rezultat1 = $zamenjenopret ; }

        if($zamenjenopret{0} == '+') { $rezultat = substr($rezultat1,1);} else { $rezultat = $rezultat1; }
        $rezultatf = str_replace('++', '+', $rezultat);
        return ($rezultatf);
    }
}
