<?php

function getBaseUrl() {
    return getenv( 'app.baseURL' );
}

function getEnvironment() {
    return getenv( 'app.environment' );
}

function getMosquittoBrokerServerIp() {
    return getenv( 'app.mqttBrokerServerIp' );
}

function getMosquittoBrokerTcpPort() {
    return getenv( 'app.mqttBrokerServerTcpPort' );
}
function getMosquittoBrokerWebSocketPort() {
    return getenv( 'app.mqttBrokerServerWebSocketPort' );
}

function getLogPath() {
    return LOG_PATH;
}

function getProjectName() {
    return PROJECT_NAME;
}

function includesHeaderFooter( $arrmixData  ) {
    echo view( 'backend/includes/header', $arrmixData );
    echo view( 'backend/' . $arrmixData['view'], $arrmixData );
    echo view( 'backend/includes/footer', $arrmixData );
}

function includesAll( $arrmixData  ) {
    echo view( 'backend/includes/header', $arrmixData );
    echo view( 'backend/includes/sidebar', $arrmixData );
    echo view( 'backend/' . $arrmixData['view'], $arrmixData );
    echo view( 'backend/includes/footer', $arrmixData );
}

function getValidationError( $strField ) {
    $arrmixValidationErrors = App\Controllers\BaseController::getFormValidationErrors();
    
    if( true == isValArr( $arrmixValidationErrors ) && true == isset( $arrmixValidationErrors[$strField] ) ) {
        $strHtml = '<span class="error invalid-feedback js-validation-error">' . $arrmixValidationErrors[$strField] . '</span>';
        return $strHtml;
    } 

    return;
}

function printLastSql() {
    $objInstance = & get_instance();
    printDie( $objInstance->db->last_query() );
}

function printFormValidationError() {
    printDie( App\Controllers\BaseController::getFormValidationErrors() );
}

function prints($data) {
    echo "<pre>";
    print_r($data);
}

function printDie($data) {
    echo "<pre>";
    print_r($data);
    die;
}

function isValArr( $arrmixData ) {
    if( !empty( $arrmixData ) && is_array( $arrmixData ) && count( $arrmixData ) > 0 ) {
        return true;
    } 
    
    return false;
}

function isValStr( $strData ) {
    if ( !empty( $strData ) && is_string( $strData ) ) {
        return true;
    } 

    return false;
}

function isValId( $intData ) {
    
    if ( !empty( $intData ) && is_numeric( $intData ) ) {
        return true;
    } 
    
    return false;
}

function isVal( $var ) {
    if ( !empty( $var ) && false == is_null( $var ) ) {
        return true;
    } 

    return false;
}

function convertDateFormatToStandardFormat( $strDate ) {
    $strDate = str_replace( '/', '-', $strDate );
    return date( 'Y-m-d', strtotime( $strDate ) );
}

function replaceDateTimeDashFormatToUnderscore( $strDateTime ) {
    $strReplaceDateTime = preg_replace( '/\b-\b/', '_', $strDateTime );
    $strReplaceDateTime = preg_replace( '/\b:\b/', '_', $strReplaceDateTime );
    $strReplaceDateTime = preg_replace( '/\b \b/', '_', $strReplaceDateTime );
    
    return $strReplaceDateTime;
}

function rekeyArray( $arrmixListData, $strColumnName, $boolHasMultipleArraysWithSameKey = false ) {

    if( true == $boolHasMultipleArraysWithSameKey ) {
        $arrmixRekyData = [];
        foreach( $arrmixListData as $arrmixData ) {
            $strKey = $arrmixData[$strColumnName];

            $arrmixRekyData[$strKey][] = $arrmixData;
        }

        return $arrmixRekyData;
    }

    return array_column( $arrmixListData, NULL, $strColumnName );
}

function generateRandomString( $intLength = 10, $boolIsRequiredNumeric = false ) {
    
    $strCharacters = '123456789abcdefghijklmnopqrstuvwxyz';
    if( true == $boolIsRequiredNumeric ) {
        $strCharacters = '123456789';
    }
    
    $intCharactersLength = strlen( $strCharacters );
    $strRandomString = '';

    for( $intCounter = 0; $intCounter < $intLength; $intCounter++ ) {
        $strRandomString .= $strCharacters[rand( 0, $intCharactersLength - 1 )];
    }

    return $strRandomString;
}

function getAdminMailData() {
    
    return [
        [
            'email_id' => ADMIN_MAIL_ID,
            'fullname' => ADMIN_NAME
        ]
    ];
}

function getPlacementMailData() {
    
    return [
        [
            'email_id' => PLACEMENT_MAIL_ID,
            'fullname' => 'Team'
        ]
    ];
}

function getSupportMailData() {
    
    return [
        [
            'email_id' => SUPPORT_MAIL_ID,
            'fullname' => 'Team'
        ]
    ];
}

function getEmailSettingDetails( $intEmailUserType = 1 ) {

    $arrmixConfig         = [];
    $arrmixConfig['host'] = SMTP_HOST;
    $arrmixConfig['port'] = SMTP_PORT;
    
    switch( $intEmailUserType ) {
        
        case EMAIL_USER_TYPE_SUPPORT:
            
            $arrmixConfig['username'] = SMTP_USERNAME_SUPPORT;
            $arrmixConfig['password'] = SMTP_PASSWORD_SUPPORT;
            break;

        case EMAIL_USER_TYPE_PLACEMENT:
            
            $arrmixConfig['username'] = SMTP_USERNAME_PLACEMENT;
            $arrmixConfig['password'] = SMTP_PASSWORD_PLACEMENT;
            break;

        case EMAIL_USER_TYPE_ADMIN:
            
            $arrmixConfig['username'] = SMTP_USERNAME_ADMIN;
            $arrmixConfig['password'] = SMTP_PASSWORD_ADMIN;
            break;

        case EMAIL_USER_TYPE_HR:
            
            $arrmixConfig['username'] = SMTP_USERNAME_HR;
            $arrmixConfig['password'] = SMTP_PASSWORD_HR;
            break;
        
        default:

            $arrmixConfig['username'] = SMTP_USERNAME_SUPPORT;
            $arrmixConfig['password'] = SMTP_PASSWORD_SUPPORT;

    }

    $arrmixConfig['protocol'] = 'smtp';
    if( ENVIRONMENT_PRODUCTION == getEnvironment() )  {
        $arrmixConfig['protocol'] = 'mail';
    } 

    return $arrmixConfig;
}

function getGeneratedByName( $intGeneratedBy ) {

    $strGeneratedByName = '';
    switch( $intGeneratedBy ) {
        
        case GENERATED_BY_SYSTEM:

            $strGeneratedByName = 'System';
            break;

        case GENERATED_BY_INTERNAL:

            $strGeneratedByName = 'Internal';
            break;
        
        case GENERATED_BY_EXTERNAL:

            $strGeneratedByName = 'External';
            break;    
        
        default:
            
            $strGeneratedByName = 'System';
            break;    
    }

    return $strGeneratedByName;
}

function getDashboardPanelColor( $intUserTypeId ) {

    switch ( $intUserTypeId ) {

        case \App\Models\UserTypesModel::USER_TYPE_SUPER_ADMIN:
        case \App\Models\UserTypesModel::USER_TYPE_ADMIN:
            
            $arrmixColorCombination = [
                'navbar'          => 'navbar-light navbar-info',
                'sidebar'         => 'sidebar-light-info',
                'brand-logo-link' => 'navbar-info',
                'accent'          => 'accent-dark'
            ];
            break;
        
        default:
            $arrmixColorCombination = [
                'navbar'          => 'navbar-light navbar-orange',
                'sidebar'         => 'sidebar-light-orange',
                'brand-logo-link' => 'navbar-orange',
                'accent'          => 'accent-orange'
            ];
            
    }

    return $arrmixColorCombination;

}