<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('getUserProfileImage')) {

    function getUserProfileImage() {
        $CI = & get_instance();
        $CI->load->model('user_model');
        $userid = $CI->session->userdata('userid');
        $data = $CI->user_model->get_profile_image($userid);

        if (!empty($data->user_image)) {
            if (file_exists('assets/images/users' . '/' . $data->user_image)) {
                return base_url('assets/images/users') . '/' . $data->user_image;
            } else {
                return base_url('assets/images/users/avatar-1.jpg');
            }
        } else {
            return base_url('assets/images/users/avatar-1.jpg');
        }
    }

}

if (!function_exists('generateColorCode')) {

    function generateColorCode() {
        $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
        $color = '#' . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)];
        return strtoupper($color);
    }

}

if (!function_exists('ColorDarken')) {

    function ColorDarken($color, $dif = 20) {

        $color = str_replace('#', '', $color);
        if (strlen($color) != 6) {
            return '000000';
        }
        $rgb = '';

        for ($x = 0; $x < 3; $x++) {
            $c = hexdec(substr($color, (2 * $x), 2)) - $dif;
            $c = ($c < 0) ? 0 : dechex($c);
            $rgb .= (strlen($c) < 2) ? '0' . $c : $c;
        }

        return '#' . $rgb;
    }

}

if (!function_exists('randomColor')) {

    function randomColor($minVal = 0, $maxVal = 255) {

        $CI = & get_instance();

// Make sure the parameters will result in valid colours
        $minVal = $minVal < 0 || $minVal > 255 ? 0 : $minVal;
        $maxVal = $maxVal < 0 || $maxVal > 255 ? 255 : $maxVal;

// Generate 3 values
        $r = mt_rand($minVal, $maxVal);
        $g = mt_rand($minVal, $maxVal);
        $b = mt_rand($minVal, $maxVal);

// Return a hex colour ID string
        $code = ColorDarken(sprintf('#%02X%02X%02X', $r / 2, $g / 2, $b / 2));
        $data = $CI->visits_model->check_color($code);

        if ($code === $data->visit_color) {
            return randomColor(0, 100);
        } else {
            return $code;
        }
    }

}

if (!function_exists('checkroles')) {

    function checkroles($user1, $user2, $user3) {
//        if($user1 == 0){
//            $CI = & get_instance();
//            if($CI->session->userdata('role') == 'Store Manager'){
//                redirect(base_url('servicetypes'));
//            }
//        }

        if ($user2 == 0) {
            $CI = & get_instance();
            if ($CI->session->userdata('role') == 'HR') {
                redirect(base_url('staff_list'));
            }
        }

        if ($user3 == 0) {
            $CI = & get_instance();
            if ($CI->session->userdata('role') == 'Reception') {
                redirect(base_url('scheduler'));
            }
        }
    }

}

if (!function_exists('convert_number_to_words')) {

    function convert_number_to_words($number) {

        $hyphen = '-';
        $conjunction = '  ';
        $separator = ' ';
        $negative = 'negative ';
        $decimal = ' point ';
        $dictionary = array(
            0 => 'Zero',
            1 => 'One',
            2 => 'Two',
            3 => 'Three',
            4 => 'Four',
            5 => 'Five',
            6 => 'Six',
            7 => 'Seven',
            8 => 'Eight',
            9 => 'Nine',
            10 => 'Ten',
            11 => 'Eleven',
            12 => 'Twelve',
            13 => 'Thirteen',
            14 => 'Fourteen',
            15 => 'Fifteen',
            16 => 'Sixteen',
            17 => 'Seventeen',
            18 => 'Eighteen',
            19 => 'Nineteen',
            20 => 'Twenty',
            30 => 'Thirty',
            40 => 'Fourty',
            50 => 'Fifty',
            60 => 'Sixty',
            70 => 'Seventy',
            80 => 'Eighty',
            90 => 'Ninety',
            100 => 'Hundred',
            1000 => 'Thousand',
            1000000 => 'Million',
            1000000000 => 'Billion',
            1000000000000 => 'Trillion',
            1000000000000000 => 'Quadrillion',
            1000000000000000000 => 'Quintillion'
        );

        if (!is_numeric($number)) {
            return false;
        }

        if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
            // overflow
            trigger_error(
                    'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
                    E_USER_WARNING
            );
            return false;
        }

        if ($number < 0) {
            return $negative . convert_number_to_words(abs($number));
        }

        $string = $fraction = null;

        if (strpos($number, '.') !== false) {
            list($number, $fraction) = explode('.', $number);
        }

        switch (true) {
            case $number < 21:
                $string = $dictionary[$number];
                break;
            case $number < 100:
                $tens = ((int) ($number / 10)) * 10;
                $units = $number % 10;
                $string = $dictionary[$tens];
                if ($units) {
                    $string .= $hyphen . $dictionary[$units];
                }
                break;
            case $number < 1000:
                $hundreds = $number / 100;
                $remainder = $number % 100;
                $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
                if ($remainder) {
                    $string .= $conjunction . convert_number_to_words($remainder);
                }
                break;
            default:
                $baseUnit = pow(1000, floor(log($number, 1000)));
                $numBaseUnits = (int) ($number / $baseUnit);
                $remainder = $number % $baseUnit;
                $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
                if ($remainder) {
                    $string .= $remainder < 100 ? $conjunction : $separator;
                    $string .= convert_number_to_words($remainder);
                }
                break;
        }

        if (null !== $fraction && is_numeric($fraction)) {
            $string .= $decimal;
            $words = array();
            foreach (str_split((string) $fraction) as $number) {
                $words[] = $dictionary[$number];
            }
            $string .= implode(' ', $words);
        }

        return $string;
    }

}