<?php
namespace App\Controllers\Backend;

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

class DeviceTcpIpSettingsController extends BackendController {
    
    public function actionView() {

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

            $strConfigFileName = TEMP_CONFIG_FILE_NAME;
            $intUsedConfigFile = USED_TEMP_CONFIG_FILE;

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

            $strConfigFileName = LATEST_CONFIG_FILE_NAME;
            $intUsedConfigFile = USED_LATEST_CONFIG_FILE;
        } else {
            // Default Config
            if( false == $this->g_objServiceLibrary->checkConfigFilePresentOrNot( DEFAULT_CONFIG_FILE_NAME ) ) {

                $this->session->setFlashdata( 'error', 'Unable to get default config. Please connect with technical team.' );
                return redirect()->route( 'dashboard' );   
            }
            $intUsedConfigFile = USED_DEFAULT_CONFIG_FILE;
            $strConfigFileName = DEFAULT_CONFIG_FILE_NAME;
        }

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

        $arrmixDeviceConfigJsonFileData = json_decode( $strConfigFileData, true );

        $arrmixTcpIpSettingsDetails = [
            'mode'               => $arrmixDeviceConfigJsonFileData['tcpip_conf']['tcpip_mode'],
            'port'               => $arrmixDeviceConfigJsonFileData['tcpip_conf']['port'],
            'connection_timeout' => $arrmixDeviceConfigJsonFileData['tcpip_conf']['conn_tout'],
            'response_timeout'   => $arrmixDeviceConfigJsonFileData['tcpip_conf']['resp_tout'],
            'ip_address'         => $arrmixDeviceConfigJsonFileData['tcpip_conf']['ip'],
            'subnet_mask'        => $arrmixDeviceConfigJsonFileData['tcpip_conf']['mask'],
            'default_gateway'    => $arrmixDeviceConfigJsonFileData['tcpip_conf']['gatw'],
        ];
        
        $arrmixBreadCrumbData['strContentHeader']     = 'TCP-IP';
        $arrmixBreadCrumbData['arrmixBreadCrumbList'] = [
            ['name' => 'TCP-IP', 'is_active' => true]
        ];
        
        $arrmixData                               = [];
        $arrmixData['strTitle']                   = 'TCP-IP';
        $arrmixData['strCardTitle']               = 'View TCP-IP';
        $arrmixData['view']                       = 'device-tcp-ip-settings/view-details';
        $arrmixData['arrmixBreadCrumbData']       = $arrmixBreadCrumbData;
        $arrmixData['arrmixTcpIpSettingsDetails'] = $arrmixTcpIpSettingsDetails;
        $arrmixData['intUsedConfigFile']          = $intUsedConfigFile;
        
        return $this->render( $arrmixData );
    }

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

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

            $arrmixInsertTcpIpSettingData = $arrmixRequestData;

            $intTcpIpSettingId = TcpIpSettingsModel::createService()->add( $arrmixInsertTcpIpSettingData );

            if( true == $intTcpIpSettingId ) {
                $this->session->setFlashdata( 'success', 'The TcpIp setting has been added successfully.' );

                return redirect()->route( 'device-settings/tcp-ip-settings/view' );
            }

            $this->session->setFlashdata( 'error', 'Unable to insert data for tcpip settings.' );
        }
        
        return $this->render( $arrmixData );
    }
    
    public function actionEdit() {
        
        $arrmixRequestData = $this->request->getGet();

        if( false == isset( $arrmixRequestData['used_config_file'] ) || false == isValId( $arrmixRequestData['used_config_file'] ) ) {
            
            $this->session->setFlashdata( 'error', 'Unable to get config file.' );
            return redirect()->route( 'device-settings/tcp-ip-settings/view' );
        }

        $intUsedConfigFile = $arrmixRequestData['used_config_file'];

        switch( $intUsedConfigFile ) {

            case USED_TEMP_CONFIG_FILE:
                $strConfigFileData = $this->g_objServiceLibrary->getConfigFile( TEMP_CONFIG_FILE_NAME );
                break;
            
            case USED_LATEST_CONFIG_FILE:
                $strConfigFileData = $this->g_objServiceLibrary->getConfigFile( LATEST_CONFIG_FILE_NAME );
                break;
            
            case USED_DEFAULT_CONFIG_FILE:
                $strConfigFileData = $this->g_objServiceLibrary->getConfigFile( DEFAULT_CONFIG_FILE_NAME );
                break;
            
            default:
                $strConfigFileData = $this->g_objServiceLibrary->getConfigFile( DEFAULT_CONFIG_FILE_NAME );
                break;
        }

        $arrmixDeviceConfigJsonFileData = json_decode( $strConfigFileData, true );
        
        $arrmixTcpIpSettingsDetails = [
            'mode'               => $arrmixDeviceConfigJsonFileData['tcpip_conf']['tcpip_mode'],
            'port'               => $arrmixDeviceConfigJsonFileData['tcpip_conf']['port'],
            'connection_timeout' => $arrmixDeviceConfigJsonFileData['tcpip_conf']['conn_tout'],
            'response_timeout'   => $arrmixDeviceConfigJsonFileData['tcpip_conf']['resp_tout'],
            'ip_address'         => $arrmixDeviceConfigJsonFileData['tcpip_conf']['ip'],
            'subnet_mask'        => $arrmixDeviceConfigJsonFileData['tcpip_conf']['mask'],
            'default_gateway'    => $arrmixDeviceConfigJsonFileData['tcpip_conf']['gatw'],
        ];

        $arrmixBreadCrumbData['strContentHeader']     = 'Edit TCP-IP';
        $arrmixBreadCrumbData['arrmixBreadCrumbList'] = [
            ['name' => 'View TCP-IP', 'is_active' => false, 'href' => getBaseUrl() . 'device-settings/tcp-ip-settings/view' ],
            ['name' => 'Edit TCP-IP', 'is_active' => true]
        ];
        
        $arrmixData                 = [];
        $arrmixData['strTitle']     = 'Edit TCP-IP';
        $arrmixData['strCardTitle'] = 'Edit TCP-IP';
        $arrmixData['view']         = 'device-tcp-ip-settings/form-details';

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

            $this->setConfigFileData( $arrmixRequestData );
            
            $this->session->setFlashdata( 'success', 'Tcp/Ip setting has been updated successfully.' );
            
            return redirect()->route( 'device-settings/tcp-ip-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( $arrmixUpdateTcpIpSettingsData ) {
    
        $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['tcpip_conf'] = [
            'tcpip_mode' => $arrmixUpdateTcpIpSettingsData['mode'],
            'port'       => $arrmixUpdateTcpIpSettingsData['port'],
            'conn_tout'  => $arrmixUpdateTcpIpSettingsData['connection_timeout'],
            'resp_tout'  => $arrmixUpdateTcpIpSettingsData['response_timeout'], 
            'ip'         => $arrmixUpdateTcpIpSettingsData['ip_address'],
            'mask'       => $arrmixUpdateTcpIpSettingsData['subnet_mask'],
            'gatw'       => $arrmixUpdateTcpIpSettingsData['default_gateway']
        ];

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

        return true;
    }
}
