<?php
namespace App\Controllers\Backend;

use App\Controllers\BackendController;
use App\Models\NetworkSettingsModel;
use App\Models\ComSettingsModel;

class BasicSettingsController extends BackendController {
    
    public function actionView() {

        $arrmixResponseConfigData = $this->getConfigData();

        $arrmixDeviceConfigJsonFileData = $arrmixResponseConfigData['device_config_josn_file_data'];
        $intUsedConfigFile              = $arrmixResponseConfigData['used_config_file'];

        $arrmixNetworkSettingsDetails = [
            'network_setting_id'   => 1,
            'device_mode'          => $arrmixDeviceConfigJsonFileData['dev_mode'],
            'mode_settings'        => $arrmixDeviceConfigJsonFileData['spt'],
            'mode_settings_status' => $arrmixDeviceConfigJsonFileData['spt_enable'],
            'eth1_mode'            => $arrmixDeviceConfigJsonFileData['eth1']['mode'],
            'eth1_ip_address'      => $arrmixDeviceConfigJsonFileData['eth1']['ip'],
            'eth1_subnet_mask'     => $arrmixDeviceConfigJsonFileData['eth1']['mask'],
            'eth1_default_gateway' => $arrmixDeviceConfigJsonFileData['eth1']['gatw'],
            'eth2_mode'            => $arrmixDeviceConfigJsonFileData['eth2']['mode'],
            'eth2_ip_address'      => $arrmixDeviceConfigJsonFileData['eth2']['ip'],
            'eth2_subnet_mask'     => $arrmixDeviceConfigJsonFileData['eth2']['mask'],
            'eth2_default_gateway' => $arrmixDeviceConfigJsonFileData['eth2']['gatw'],
        ];
        
        $arrmixComSettingsDetails = [
            'baud_rate'      => $arrmixDeviceConfigJsonFileData['rs485']['br'],
            'data_bits'      => $arrmixDeviceConfigJsonFileData['rs485']['data_bit'],
            'parity_bits'    => $arrmixDeviceConfigJsonFileData['rs485']['par_bit'],
            'flow_control'   => $arrmixDeviceConfigJsonFileData['rs485']['fl_cntr'],
            'stop_bits'      => $arrmixDeviceConfigJsonFileData['rs485']['stop_bit']
        ];

        $arrmixBreadCrumbData['strContentHeader']     = 'Basic Settings';
        $arrmixBreadCrumbData['arrmixBreadCrumbList'] = [
            ['name' => 'Basic Settings', 'is_active' => true]
        ];
        
        $arrmixData                                 = [];
        $arrmixData['strTitle']                     = 'Basic Settings';
        $arrmixData['strCardTitle']                 = 'View Basic Settings';
        $arrmixData['view']                         = 'basic-settings/view-details';
        $arrmixData['arrmixBreadCrumbData']         = $arrmixBreadCrumbData;
        $arrmixData['arrmixNetworkSettingsDetails'] = $arrmixNetworkSettingsDetails;
        $arrmixData['arrmixComSettingsDetails']     = $arrmixComSettingsDetails;
        $arrmixData['intUsedConfigFile']            = $intUsedConfigFile;
        
        return $this->render( $arrmixData );
    }

    
    public function actionAdd() {
        
        $arrmixBreadCrumbData['strContentHeader']     = 'Basic Settings';
        $arrmixBreadCrumbData['arrmixBreadCrumbList'] = [
            ['name' => 'Add Basic Settings', 'is_active' => true]
        ];

        $arrmixData                         = [];
        $arrmixData['strTitle']             = 'Add Basic Settings';
        $arrmixData['strCardTitle']         = 'Add Basic Settings';
        $arrmixData['view']                 = 'basic-settings/form-details';
        $arrmixData['arrmixBreadCrumbData'] = $arrmixBreadCrumbData;
        
        if( $this->request->getPost() ) {
            $arrmixRequestData = $this->request->getPost();
            
            if( false == $this->validation->run( $arrmixRequestData['network_settings'] , 'validateNetworkSettings' ) ) { 
                
                $this->session->setFlashdata( 'error', 'Validation Errors. Please check the below errors of network settings.' );
                self::setFormValidationErrors( $this->validation->getErrors() );
                
                return $this->render( $arrmixData );
            }

            if( false == $this->validation->run( $arrmixRequestData['com_settings'] , 'validateComSettings' ) ) { 
                
                $this->session->setFlashdata( 'error', 'Validation Errors. Please check the below errors of com settings.' );
                self::setFormValidationErrors( $this->validation->getErrors() );
                
                return $this->render( $arrmixData );
            }

            $this->setConfigFileData( $arrmixRequestData['network_settings'], $arrmixRequestData['com_settings'] );
        }
        
        return $this->render( $arrmixData );
    }
    
    public function actionEdit() {
        
        $arrmixResponseConfigData = $this->getConfigData();


        $arrmixDeviceConfigJsonFileData = $arrmixResponseConfigData['device_config_josn_file_data'];
        $intUsedConfigFile              = $arrmixResponseConfigData['used_config_file'];

        $arrmixNetworkSettingsDetails = [
            'device_mode'          => $arrmixDeviceConfigJsonFileData['dev_mode'],
            'mode_settings'        => $arrmixDeviceConfigJsonFileData['spt'],
            'mode_settings_status' => $arrmixDeviceConfigJsonFileData['spt_enable'],
            'eth1_mode'            => $arrmixDeviceConfigJsonFileData['eth1']['mode'],
            'eth1_ip_address'      => $arrmixDeviceConfigJsonFileData['eth1']['ip'],
            'eth1_subnet_mask'     => $arrmixDeviceConfigJsonFileData['eth1']['mask'],
            'eth1_default_gateway' => $arrmixDeviceConfigJsonFileData['eth1']['gatw'],
            'eth2_mode'            => $arrmixDeviceConfigJsonFileData['eth2']['mode'],
            'eth2_ip_address'      => $arrmixDeviceConfigJsonFileData['eth2']['ip'],
            'eth2_subnet_mask'     => $arrmixDeviceConfigJsonFileData['eth2']['mask'],
            'eth2_default_gateway' => $arrmixDeviceConfigJsonFileData['eth2']['gatw'],
        ];
        
        $arrmixComSettingsDetails = [
            'baud_rate'      => $arrmixDeviceConfigJsonFileData['rs485']['br'],
            'data_bits'      => $arrmixDeviceConfigJsonFileData['rs485']['data_bit'],
            'parity_bits'    => $arrmixDeviceConfigJsonFileData['rs485']['par_bit'],
            'flow_control'   => $arrmixDeviceConfigJsonFileData['rs485']['fl_cntr'],
            'stop_bits'      => $arrmixDeviceConfigJsonFileData['rs485']['stop_bit']
        ];
        
        $arrmixBreadCrumbData['strContentHeader']     = 'Edit Basic Settings';
        $arrmixBreadCrumbData['arrmixBreadCrumbList'] = [
            ['name' => 'View Basic Setting', 'is_active' => false, 'href' => getBaseUrl() . 'basic-settings/view' ],
            ['name' => 'Edit', 'is_active' => true]
        ];
        
        $arrmixData                 = [];
        $arrmixData['strTitle']     = 'Edit Basic Setting';
        $arrmixData['strCardTitle'] = 'Edit Basic Setting';
        $arrmixData['view']         = 'basic-settings/form-details';

        $arrmixData['arrmixBreadCrumbData']         = $arrmixBreadCrumbData;
        $arrmixData['arrmixNetworkSettingsDetails'] = $arrmixNetworkSettingsDetails;
        $arrmixData['arrmixComSettingsDetails']     = $arrmixComSettingsDetails;
        $arrmixData['intUsedConfigFile']            = $intUsedConfigFile;
        
        if( true == isValArr( $this->request->getPost() ) ) {
            
            $arrmixRequestData = $this->request->getPost();
            
            if( false == $this->validation->run( $arrmixRequestData['network_settings'] , 'validateNetworkSettings' ) ) { 
                
                $this->session->setFlashdata( 'error', 'Validation Errors. Please check the below errors of network settings.' );
                self::setFormValidationErrors( $this->validation->getErrors() );
                
                return $this->render( $arrmixData );
            }

            if( false == $this->validation->run( $arrmixRequestData['com_settings'] , 'validateComSettings' ) ) { 
                
                $this->session->setFlashdata( 'error', 'Validation Errors. Please check the below errors of com settings.' );
                self::setFormValidationErrors( $this->validation->getErrors() );
                
                return $this->render( $arrmixData );
            }

            $arrmixUpdateNetworkSettingsData = $arrmixRequestData['network_settings'];
            $arrmixUpdateComSettingsData     = $arrmixRequestData['com_settings'];

            $this->setConfigFileData( $arrmixUpdateNetworkSettingsData, $arrmixUpdateComSettingsData );

            $this->session->setFlashdata( 'success', 'Basic setting has been updated successfully.' );
            
            return redirect()->route( 'basic-settings/view' );        
        }
        
        return $this->render( $arrmixData );
    }
    
    public function actionDelete() {
        
        $arrmixRequestData = $this->request->getPost();
        
        if( false == isset( $arrmixRequestData['category_id'] ) || false == isValId( $arrmixRequestData['category_id'] ) ) {
            $arrmixResponseData['success'] = false;
            $arrmixResponseData['message'] = 'Invalid Category - ' . $arrmixRequestData['category_id'];
            
            $this->response( $arrmixResponseData );
            return true;
        }

        $arrmixResponseData['success'] = false;
        $arrmixResponseData['message'] = 'Unable to delete the category - ' . $arrmixRequestData['category_id'];

        $boolResult = CategoriesModel::createService()->delete( $arrmixRequestData['category_id'] );
        if( true == $boolResult ) { 
            $arrmixResponseData['success'] = true;
            $arrmixResponseData['message'] = 'Category has been successfully deleted.';
        } 
        
        $this->response( $arrmixResponseData );
    }  

    public function setConfigFileData( $arrmixUpdateNetworkSettingsData, $arrmixUpdateComSettingsData ) {
    
        $arrmixDeviceConfigJsonFileData = [];

        if( true == $this->g_objServiceLibrary->checkConfigFilePresentOrNot( TEMP_CONFIG_FILE_NAME ) ) {
            $strConfigFileData = $this->g_objServiceLibrary->getConfigFile( TEMP_CONFIG_FILE_NAME );

            if( true == isVal( $strConfigFileData ) ) {
                $arrmixDeviceConfigJsonFileData = json_decode( $strConfigFileData, true );
            }
        } else if( true == $this->g_objServiceLibrary->checkConfigFilePresentOrNot( LATEST_CONFIG_FILE_NAME ) ) {

            $strConfigFileData = $this->g_objServiceLibrary->getConfigFile( LATEST_CONFIG_FILE_NAME );

            if( true == isVal( $strConfigFileData ) ) {
                $arrmixDeviceConfigJsonFileData = json_decode( $strConfigFileData, true );
            }
        } else {
            $strConfigFileData = $this->g_objServiceLibrary->getConfigFile( DEFAULT_CONFIG_FILE_NAME );

            if( true == isVal( $strConfigFileData ) ) {
                $arrmixDeviceConfigJsonFileData = json_decode( $strConfigFileData, true );
            }
        }

        $arrmixDeviceConfigJsonFileData['dev_mode']   = $arrmixUpdateNetworkSettingsData['device_mode'];
        $arrmixDeviceConfigJsonFileData['spt']        = $arrmixUpdateNetworkSettingsData['mode_settings'];
        $arrmixDeviceConfigJsonFileData['spt_enable'] = $arrmixUpdateNetworkSettingsData['mode_settings_status'];
        
        $arrmixDeviceConfigJsonFileData['eth1'] = [
            'mode' => $arrmixUpdateNetworkSettingsData['eth1_mode'],
            'ip'   => $arrmixUpdateNetworkSettingsData['eth1_ip_address'],
            'gatw' => $arrmixUpdateNetworkSettingsData['eth1_default_gateway'],
            'mask' => $arrmixUpdateNetworkSettingsData['eth1_subnet_mask'],
        ];
        $arrmixDeviceConfigJsonFileData['eth2'] = [
            'mode' => $arrmixUpdateNetworkSettingsData['eth2_mode'],
            'ip'   => $arrmixUpdateNetworkSettingsData['eth2_ip_address'],
            'gatw' => $arrmixUpdateNetworkSettingsData['eth2_default_gateway'],
            'mask' => $arrmixUpdateNetworkSettingsData['eth2_subnet_mask'],
        ];

        $arrmixDeviceConfigJsonFileData['rs485'] = [
            'rs485_conf' => $arrmixDeviceConfigJsonFileData['rs485']['rs485_conf'],
            'br'         => $arrmixUpdateComSettingsData['baud_rate'],
            'par_bit'    => $arrmixUpdateComSettingsData['parity_bits'],
            'data_bit'   => $arrmixUpdateComSettingsData['data_bits'],
            'stop_bit'   => $arrmixUpdateComSettingsData['stop_bits'],
            'fl_cntr'    => 1,
        ];

        $this->g_objServiceLibrary->updateConfigFile( json_encode( $arrmixDeviceConfigJsonFileData ) );

        return true;
    }
}
