<?php
namespace App\Controllers\MqttServices;

use App\Controllers\MqttServices\Auth\MqttServiceAuth;
use Models\DeviceUpgradesModel;
use Models\UserDeviceInfoModel;

class UsersController extends MqttServiceAuth {

    
    public function actionStatus() {
        
        if( false == $this->validation->run( $this->g_arrmixRequestParameterDetails, 'validateStatus' ) ) { 
            $this->generateMqttError( 'data', ERROR_INVALID_PARAMETERS, implode( ',', $this->validation->getErrors() ), ERROR_TYPE_MESSAGE_INVALID_PARAMETERS );
        }

        $strMsgId     = $this->g_arrmixRequestParameterDetails['msgid'];
        $arrmixStatusResponse = $this->processStatus( $strMsgId );

        $arrmixMessageData = [
            'epoach'    => strtotime( date( 'Y-m-d H:i:s' ) ),
            'msgid'     => $strMsgId,
            'msgstatus' => $arrmixStatusResponse['msgstatus']
        ];

        $arrmixReceivedTopic =  explode( "/", $this->g_strReceivedTopic );

        $arrmixResponseData['is_require_publish'] = $arrmixStatusResponse['is_require_publish'];
        $arrmixResponseData['topic']              = self::MQTT_TOPIC . 'pub/' . $arrmixReceivedTopic[3] . '/ondemand';
        $arrmixResponseData['message']            = $arrmixMessageData;

        return $arrmixResponseData;
    }

    private function processStatus( $strMsgId ) {

        
        switch( $strMsgId ) {

            case self::MSGID_REQDATE:
                $arrmixStatusResponse['is_require_publish'] = true;
                $arrmixStatusResponse['msgstatus']          = 0;
                break;

            case self::MSGID_FOTA:
                $arrmixRequestParameterFotaDetails = $this->g_arrmixRequestParameterDetails['fota'];

                $arrmixUpdateData = [
                    'user_id'         => $this->g_arrmixDecryptedTokenDetails['user_id'],
                    'status'          => $arrmixRequestParameterFotaDetails['stat'],
                    'current_version' => $arrmixRequestParameterFotaDetails['ver'],
                    'download'        => $arrmixRequestParameterFotaDetails['download']
                ];

                DeviceUpgradesModel::createService()->edit( 'user_id', $arrmixUpdateData );
                
                $arrmixStatusResponse['is_require_publish'] = false;
                $arrmixStatusResponse['msgstatus']          = 0;
                break;

            case self::MSGID_CONF:
                $arrmixRequestParameterConfDetails = $this->g_arrmixRequestParameterDetails['conf'];

                $arrmixUpdateData = [
                    'user_id'           => $this->g_arrmixDecryptedTokenDetails['user_id'],
                    'config_status'     => $arrmixRequestParameterConfDetails['stat'],
                    'config_error_code' => $arrmixRequestParameterConfDetails['error']
                ];

                UserDeviceInfoModel::createService()->edit( 'user_id', $arrmixUpdateData );

                $arrmixStatusResponse['is_require_publish'] = false;
                $arrmixStatusResponse['msgstatus']          = 0;
                break;
            
            case self::MSGID_REQDATA:
                
                $strMessageStatus                     = $this->g_arrmixRequestParameterDetails['msgstatus'];
                $arrmixRequestParameterReqDataDetails = $this->g_arrmixRequestParameterDetails['reqdata'];

                if( 'write' == $strMessageStatus ) {
                    
                    $arrmixUpdateUserDeviceInfoData = [
                        'user_id'               => $this->g_arrmixDecryptedTokenDetails['user_id'],
                        'data_interval'         => $arrmixRequestParameterReqDataDetails['d_hb'],
                        'heartbeat_interval'    => $arrmixRequestParameterReqDataDetails['h_hb'],
                        'is_restart'            => $arrmixRequestParameterReqDataDetails['rst'],
                        'rtc'                   => $arrmixRequestParameterReqDataDetails['rtc_s'],
                        'pump_status'           => $arrmixRequestParameterReqDataDetails['pmp_s'],
                        'is_device_logs_needed' => $arrmixRequestParameterReqDataDetails['d_logs'],
                    ];
            
                    UserDeviceInfoModel::createService()->edit( 'user_id', $arrmixUpdateUserDeviceInfoData );
                }

                $arrmixStatusResponse['is_require_publish'] = true;
                $arrmixStatusResponse['msgstatus']          = 0;
                break;
            
            case self::MSGID_DBEAT:
                $arrmixRequestParameterDbeatDetails = $this->g_arrmixRequestParameterDetails['dbeat'];

                $arrmixUpdateData = [
                    'user_id'          => $this->g_arrmixDecryptedTokenDetails['user_id'],
                    'dbeat_status'     => $arrmixRequestParameterDbeatDetails['stat'],
                    'dbeat_error_code' => $arrmixRequestParameterDbeatDetails['error']
                ];

                UserDeviceInfoModel::createService()->edit( 'user_id', $arrmixUpdateData );

                $arrmixStatusResponse['is_require_publish'] = false;
                $arrmixStatusResponse['msgstatus']          = 0;
                break;

            default:
                $arrmixStatusResponse['is_require_publish'] = false;
                $arrmixStatusResponse['msgstatus']          = 0;
                break;
            
        }

        return $arrmixStatusResponse;
        
    }
}
