<?php
namespace App\Controllers\Backend;

use App\Controllers\BackendController;
use App\Models\UsersModel;

class DashboardController extends BackendController {
    
    public function actionDashboard() {

        $boolIsTempConfigFilePresent = false;
        if( true == $this->g_objServiceLibrary->checkConfigFilePresentOrNot( TEMP_CONFIG_FILE_NAME ) ) {
            $boolIsTempConfigFilePresent = true;
        }

        $arrmixResponseConfigData = $this->getConfigData();

        $arrmixDeviceConfigJsonFileData = $arrmixResponseConfigData['device_config_josn_file_data'];

        $arrmixBreadCrumbData['strContentHeader'] = 'Dashboard';
        
        $arrmixDashboardData = [];
        
        $arrmixData                                   = [];
        $arrmixData['strTitle']                       = 'Dashboard';
        $arrmixData['view']                           = 'dashboard/super-admin';
        $arrmixData['arrmixBreadCrumbData']           = $arrmixBreadCrumbData;
        $arrmixData['arrmixDashboardData']            = $arrmixDashboardData;
        $arrmixData['boolIsTempConfigFilePresent']    = $boolIsTempConfigFilePresent;
        $arrmixData['arrmixDeviceConfigJsonFileData'] = $arrmixDeviceConfigJsonFileData;
        
        return $this->render( $arrmixData );
    }   

    public function actionSendConfigData() {

        $strTempConfigFileFullPath   = CONFIG_FILE_PATH . TEMP_CONFIG_FILE_NAME;
        $strLatestConfigFileFullPath = CONFIG_FILE_PATH . LATEST_CONFIG_FILE_NAME;

        if( true == $this->g_objServiceLibrary->checkConfigFilePresentOrNot( LATEST_CONFIG_FILE_NAME ) ) {

            $strChangedLatestConfigFileName = replaceDateTimeDashFormatToUnderscore( CURRENT_DATETIME ) . '_' . LATEST_CONFIG_FILE_NAME;
    
            rename( $strLatestConfigFileFullPath, CONFIG_FILE_PATH . 'latest-config-backup/' . $strChangedLatestConfigFileName );
        }
        
        rename( $strTempConfigFileFullPath, $strLatestConfigFileFullPath );

        $strPublishMessage = [
            'method' => 'resp',
            'msg_id' => REQ_MSG_ID_1008
        ];

        $this->publishMqttData( self::MQTT_PUBLISH_TOPIC, json_encode( $strPublishMessage ) );

        $arrmixResponseData = [
            'status'  => true, 
            'message' => 'Message has been sent successfully'
        ];

        $this->response( $arrmixResponseData );
    }

    public function actionResetConfigData() {

        $strTempConfigFileFullPath   = CONFIG_FILE_PATH . TEMP_CONFIG_FILE_NAME;

        if( false == file_exists( $strTempConfigFileFullPath ) ) { 
            
            $arrmixResponseData = [
                'status'  => false, 
                'message' => 'Unable to get your latest config changes.'
            ];

            $this->response( $arrmixResponseData );
        }

        if( true == unlink( $strTempConfigFileFullPath ) ) {

            $arrmixResponseData = [
                'status'  => true, 
                'message' => 'Your config changes have been reset successfully.'
            ]; 
            
        } else {
            
            $arrmixResponseData = [
                'status'  => false, 
                'message' => 'Unable to reset your config changes.'
            ];
        }

        $this->response( $arrmixResponseData );
    }
}
