<?php
namespace App\Services;

use App\Models\Country;
use App\Models\Draft;
use App\Models\DraftType;
use App\Models\IndustrySector;
use App\Models\Interest;
use App\Models\TravelClass;
use App\Models\TravelCountry;
use App\Models\TravelProgram;
use Form;

class HTMLHelper
{


    private static $months = [
        1 => 'Januar',
        2 => 'Februar',
        3 => 'März',
        4 => 'April',
        5 => 'Mai',
        6 => 'Juni',
        7 => 'Juli',
        8 => 'August',
        9 => 'September',
        10 => 'Oktober',
        11 => 'November',
        12 => 'Dezember',
    ];

    private static $days = [
        0 => 'Sonntag',
        1=> 'Montag',
        2 => 'Dienstag',
        3 => 'Mittwoch',
        4 => 'Donnerstag',
        5 => 'Freitag',
        6 => 'Samstag',
    ];


    private static $roles = [
        0 => 'Kunde',
        1 => 'Admin',
        2 => 'SuperAdmin',
    ];


    public static function getMonth($i){
        return self::$months[intval($i)];
    }

    public static function getDay($i){
        return self::$days[intval($i)];
    }

    public static function getRoleLabel($role_id = 0){
        return '<span class="badge  badge-pill '.self::getLable($role_id).'">'.self::$roles[$role_id].'</span>';
    }

    public static function getLable($id){
        switch ($id) {
            case 0:
                return 'badge-default';
                break;
            case 1:
                return 'badge-warning';
                break;
            case 2:
                return 'badge-primary';
                break;
        }

    }

    public static function getRolesOptions(){
        $ret = "";
        foreach (self::$roles as $role_id => $value){
            $ret .= '<option value="'.$role_id.'">'.$value.'</option>\n';
        }
        return $ret;
    }



    public static function getYearSelectOptions(){
        $start = date("Y", strtotime("-5 years", time()));
        $end = date("Y", strtotime("+1 years", time()));
        $values = range($start, $end);
        $now = date("Y", time());
        $ret = "";
        foreach ($values as $value){
            $attr = ($value == $now) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$value.'" '.$attr.'>'.$value.'</option>\n';
        }
        return $ret;
    }

   public static function getDraftTypes($setId = false){
        $options = DraftType::all()->sortByDesc('id');
        $ret = "";
        foreach ($options as $option){
            $attr = ($option->id == $setId) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$option->id.'" '.$attr.'>'.$option->name.'</option>\n';
        }
        return $ret;
    }

    public static function getRangeOptions($id = false, $range = 30, $name = ""){

        $range = range(1, $range);
        $ret = "";

        foreach ($range as $item){
            $attr = ($item === $id) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$item.'" '.$attr.'>'.$item.$name.'</option>\n';

        }
        return $ret;
    }

    public static function getDraftOptions($setId = false){

        $options = Draft::where('active', 1)->orderBy('id', 'DESC')->get();
        $ret = "";
        foreach ($options as $option){
            $attr = ($option->id === $setId) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$option->id.'" '.$attr.'>'.$option->name.'</option>\n';
        }
        return $ret;
    }

    public static function getTravelClassOptions($programId = false, $setId = false){
        $options = TravelClass::where('program_id', $programId)->get();
        $ret = '<option value="">alle Kategorien</option>\n';
        foreach ($options as $option){
            $attr = ($option->id === $setId) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$option->id.'" '.$attr.'>'.$option->name.'</option>\n';
        }
        return $ret;
    }

    public static function getTravelCountriesOptions($countryId = false){
        $options = TravelCountry::where('active_backend',1)->get();
        $ret = '';
        foreach ($options as $option){
            $attr = ($option->crm_id === $countryId) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$option->crm_id.'" '.$attr.'>'.$option->name.'</option>\n';
        }
        return $ret;
    }



    public static function getWeekdaysOptions($programId = false, $weekdays = []){
        if($programId){
            $tp = TravelProgram::findOrFail($programId);
            $options = explode(',', $tp->weekdays);
        }else{
            $options = range(0, 6);
        }
        $ret = '<option value="">alle Wochentage</option>\n';
        foreach ($options as $option){
            $attr = (in_array($option, $weekdays)) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$option.'" '.$attr.'>'.self::getDay($option).'</option>\n';
        }
        return $ret;
    }

    public static function getWeekdaysString($weekdays = []){

        $ret = "";
        if(count($weekdays)){
            foreach($weekdays as $weekday){
                if($weekday !== NULL){
                    $ret .= self::getDay($weekday).', ';
                }
            }
            if($ret != ""){
                return rtrim($ret, ", ");
            }
        }
        return "alle Wochentage";
    }

    public static function getParentBy($obj = NULL, $options, $empty=true){

        $ret = "";
        $setId = 0;
        if($obj){
            $setId =  $obj->id;
        }
        if($empty){
           // $ret .= '<option value="">Main (No-Parent)</option>\n';
        }
        foreach ($options as $id => $name){
            $attr = ($id === $setId) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$id.'" '.$attr.'>'.$name.'</option>\n';
        }
        return $ret;
    }


    public static function filterHTML($html, $arg = [], $save = false){

        $dom = new \DOMDocument('1.0', 'UTF-8');
        //@$dom->loadHTML();

        @$dom->loadHTML( mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
        if(isset($arg['src'])){

            if(in_array('addHost', $arg['src'])){
                $images = $dom->getElementsByTagName('img');
                foreach ($images as $image) {

                    $src = $image->getAttribute('src');
                    $host = config('app.url_stern');
                    if(strpos($src, $host) === false){
                        $src = $host."/".ltrim($src, '/');
                    }
                    $image->setAttribute('src', $src);
                }
            }

            if(in_array('removeHost', $arg['src'])){
                $images = $dom->getElementsByTagName('img');
                foreach ($images as $image) {
                    $src = $image->getAttribute('src');
                    $host = config('app.url_stern');
                    if(strpos($src, $host) !== false){
                        $src = str_replace($host, '', $src);
                    }
                    $image->setAttribute('src', $src);
                }
            }
        }
        $html = $dom->saveHTML();

        $html = preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', $html);

        if($save){
            return mb_convert_encoding($html, "UTF-8", "HTML-ENTITIES");
        }

        return $html;
    }

    /*
   public static function getIndustrySectorsWithoutParents($id = false, $sameId = false, $all = true){
       $values = IndustrySector::where('parent_id', null)->get();
       $ret = "";
       if($all){
           $ret .= '<option value="">'.__('no').'</option>\n';
       }
       foreach ($values as $value){
           if($sameId == $value->id){
               continue;
           }
           $attr = ($value->id == $id) ? 'selected="selected"' : '';
           $ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
       }
       return $ret;
   }

   public static function getContriesWithMore($id, $all=true){#
       $values = Country::all();
       $counter = 1;
       $ret = "";
       if($all){
           $ret .= '<option value="">'.__('please select').'</option>\n';

       }
       foreach ($values as $value){
           if( $counter == 7){
               $ret .= '<optgroup label="'.__('further countrie').'">';
           }
           $attr = ($value->id == $id) ? 'selected="selected"' : '';
           $ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->getLocated().'</option>\n';

           $counter ++;
       }
       $ret .= '</optgroup>';
       return $ret;
   }

   public static function getContriesCodes($id, $all=true){#
       $values = Country::all();
       $counter = 1;
       $ret = "";
       if($all){
           $ret .= '<option value="">'.__('please select').'</option>\n';

       }
       foreach ($values as $value){

           if(!$value->phone) continue;
           if( $counter == 7){
               $ret .= '<optgroup label="'.__('further countrie').'">';
           }
           $attr = ($value->id == $id) ? 'selected="selected"' : '';
           $ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->phone.'('.$value->getLocated().')</option>\n';

           $counter ++;
       }
       $ret .= '</optgroup>';
       return $ret;
   }


   public static function getSalutation($id){
       $values = array('mr' => __('MR'), 'ms' => __('MS'));
       $ret = "";
       $ret .= '<option value="">'.__('please select').'</option>\n';
       foreach ($values as $key => $value){
           $attr = ($key == $id) ? 'selected="selected"' : '';
           $ret .= '<option value="'.$key.'" '.$attr.'>'.$value.'</option>\n';
       }
       return $ret;
   }

   public static function getSalutationLang($id){
       $values = array('mr' => __('MR'), 'ms' => __('MS'));
       return (!empty($values[$id]) ? $values[$id] : '');
   }
       */
}