<?php
namespace App\Controllers;

use App\Controllers\BaseController;

require_once APPPATH . 'Libraries/ServiceErrors.php';

class ScriptController extends BaseController {

    public $g_boolIsMqttValidateRequest;
    public $g_strPayload;
    public $g_strMethodName;
    public $g_strMessageId;
    public $g_arrmixAuthDetails;
    public $g_arrmixRequestParameterDetails;
    public $g_arrmixRequestRawData;

    public function validateMqttRequestData( $objRequestRawData ) {
        
        $this->g_arrmixRequestRawData = json_decode( $objRequestRawData, true );
        
        /* if( false == isset( $this->g_arrmixRequestRawData['payload'] ) ) {
            $this->generateMqttError( 'info', ERROR_INVALID_REQUEST, 'Payload is required.', ERROR_TYPE_MESSAGE_INVALID_REQUEST );
        }

        $this->setPayload( $this->g_arrmixRequestRawData['payload'] );

        if( false == isset( $this->g_arrmixRequestRawData['auth'] ) ) {
            $this->generateMqttError( 'info', ERROR_INVALID_REQUEST, 'Auth is required.', ERROR_TYPE_MESSAGE_INVALID_REQUEST );
        }
        
        $this->setRequestAuthDetails( $this->g_arrmixRequestRawData['auth'] ); */
        
        if( false == isset( $this->g_arrmixRequestRawData['method'] ) ) {
            $this->generateMqttError( 'info', ERROR_INVALID_REQUEST, 'Method is required.', ERROR_TYPE_MESSAGE_INVALID_REQUEST );
        }
        
        $this->setRequestMethodName( $this->g_arrmixRequestRawData['method'] );

        if( false == isset( $this->g_arrmixRequestRawData['msg_id'] ) ) {
            $this->generateMqttError( 'info', ERROR_INVALID_REQUEST, 'MsgId is required.', ERROR_TYPE_MESSAGE_INVALID_REQUEST );
        }
        
        $this->setMessageId( $this->g_arrmixRequestRawData['msg_id'] );
        
        /* if( false == isset( $this->g_arrmixRequestRawData['params'] ) ) {
            $this->generateMqttError( 'info', ERROR_INVALID_REQUEST, 'Parameters is required.', ERROR_TYPE_MESSAGE_INVALID_REQUEST );
        }

        $this->setRequestParameterDetails( $this->g_arrmixRequestRawData['params'] ); */


        
        $this->g_boolIsMqttValidateRequest = true;
    }

    public function setMessageId( $strMessageId ) {
        $this->g_strMessageId = $strMessageId;
    }

    public function setPayload( $strPayload ) {
        $this->g_strPayload = $strPayload;
    }

    public function setRequestAuthDetails( $arrmixAuthDetails ) {
        $this->g_arrmixAuthDetails = $arrmixAuthDetails;
    }

    public function setRequestParameterDetails( $arrmixParameterDetails ) {
        $this->g_arrmixRequestParameterDetails = $arrmixParameterDetails;
    }

    public function setRequestMethodName( $strMethodName ) {

        $strConvertMethodName = 'action' . str_replace( '_', '', ucwords( $strMethodName, '_' ) );
        
        $this->g_strMethodName = $strConvertMethodName;
    }
    
    public function getRequestMethodName() {
        return $this->g_strMethodName;
    }
    
    public function setRequestOriginalMethodName( $strMethodName ) {
        $this->g_strOriginalMethodName = $strMethodName;
    }

    public static function createService() {
        $strClassName = static::class;
        
        return new $strClassName();
    }    
    
}
