<?php
namespace WpBookingHotelAdminWooPanel\app\helpers;

use WpBookingHotel\Factory;
use WpBookingHotel\Filesystem\File;
use WpBookingHotel\Utilities\Utility;

class AdminHelper
{
    public static $list_key_hotel_properties=array(
        'destination'=>"destination",
        'roomtype'=>'roomtype',
        'discount'=>'discount',
        'service'=>'service',
        'tag'=>'tag',
        'rate'=>'rate',
    );
    public static function getWpBookingDestinationKey(){
        return self::$list_key_hotel_properties['destination'];
    }
    public static function getWpBookingRoomtypeKey(){
        return self::$list_key_hotel_properties['roomtype'];
    }
    public static function getWpBookingServiceKey(){
        return self::$list_key_hotel_properties['service'];
    }
    public static function getWpBookingDiscountKey(){
        return self::$list_key_hotel_properties['discount'];
    }
    public static function getWpBookingTagKey(){
        return self::$list_key_hotel_properties['tag'];
    }
    public static function getWpBookingRateKey(){
        return self::$list_key_hotel_properties['rate'];
    }

    public static function getListKeyOfTourProperties(){
        return static::$list_key_hotel_properties;
    }
    public static function deletePropertyByKey($key='',$id=0) {
        $db= Factory::getDBO();
        $query=$db->getQuery(true);
        $query->clear()
            ->delete(Utility::getTableName('hotelproperty'))
            ->where('hotel_id=' . (int)$id)
            ->where('type=' . $query->quote($key))
        ;
        $db->setQuery($query)->execute();

    }
    public static function insertNewPropertyByKey($key='', $properties=array(), $hotel_id=0) {
        $db = Factory::getDBO();
        $query =$db->getQuery(true);
        foreach ($properties as $property) {
            $query->clear()
                ->insert(Utility::getTableName('hotelproperty'))
                ->set('object_id='.(int)$property)
                ->set('type='.$query->quote($key))
                ->set('hotel_id='.(int)$hotel_id)
            ;
            $db->setQuery($query)->execute();
        }
    }

    public static function getDirContents($dir, &$results = array(), $exclude = array(), $filter = array())
    {
        $files = scandir($dir);
        $filter = array_map('strtolower', $filter);
        foreach ($files as $key => $value) {
            if (in_array($value, $exclude)) {
                continue;
            }
            $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
            $extension = strtolower(File::getExt($path));
            if (!is_dir($path)) {
                if (empty($filter)) {
                    $results[] = $path;
                } elseif (in_array($extension, $filter)) {
                    $results[] = $path;
                }
            } else {
                if ($value != "." && $value != "..") {
                    self::getDirContents($path, $results, $exclude, $filter);
                    $results[] = $path;
                }
            }
        }
        return $results;
    }


}