<?php
namespace App\Controllers\Backend;

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

class SpanningTreeController 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 );

        $arrmixNetworkSettingsDetails = [
            'device_mode'          => $arrmixDeviceConfigJsonFileData['dev_mode'],
            'mode_settings'        => $arrmixDeviceConfigJsonFileData['spt'],
            'mode_settings_status' => $arrmixDeviceConfigJsonFileData['spt_enable']
        ];

        $arrmixSpanningTreeDetails = [
            'priority'       => $arrmixDeviceConfigJsonFileData['spt_conf']['prio'],
            'maximum_age'    => $arrmixDeviceConfigJsonFileData['spt_conf']['max_age'],
            'hello_time'     => $arrmixDeviceConfigJsonFileData['spt_conf']['hello_tm'],
            'forward_delay'  => $arrmixDeviceConfigJsonFileData['spt_conf']['fwrd_delay'],
            'primary_port'   => $arrmixDeviceConfigJsonFileData['spt_conf']['pr_port'],
            'secondary_port' => $arrmixDeviceConfigJsonFileData['spt_conf']['scnd_port'],
        ];
        
        $arrmixBreadCrumbData['strContentHeader']     = 'Spanning Tree';
        $arrmixBreadCrumbData['arrmixBreadCrumbList'] = [
            ['name' => 'Spanning Tree', 'is_active' => true]
        ];
        
        $arrmixData                                 = [];
        $arrmixData['strTitle']                     = 'Spanning Tree';
        $arrmixData['strCardTitle']                 = 'View Spanning Tree';
        $arrmixData['view']                         = 'spanning-tree/view-details';
        $arrmixData['arrmixBreadCrumbData']         = $arrmixBreadCrumbData;
        $arrmixData['arrmixNetworkSettingsDetails'] = $arrmixNetworkSettingsDetails;
        $arrmixData['arrmixSpanningTreeDetails']    = $arrmixSpanningTreeDetails;
        $arrmixData['intUsedConfigFile']            = $intUsedConfigFile;
        
        return $this->render( $arrmixData );
    }

    
    public function actionAdd() {
        
        $arrmixNetworkSettingsDetails = NetworkSettingsModel::createService()->first();

        $arrmixBreadCrumbData['strContentHeader']     = 'Spanning Tree Settings';
        $arrmixBreadCrumbData['arrmixBreadCrumbList'] = [
            ['name' => 'Add Spanning Tree Settings', 'is_active' => true]
        ];

        $arrmixData                                 = [];
        $arrmixData['strTitle']                     = 'Add Spanning Tree Settings';
        $arrmixData['strCardTitle']                 = 'Add Spanning Tree Settings';
        $arrmixData['view']                         = 'spanning-tree/form-details';
        $arrmixData['arrmixBreadCrumbData']         = $arrmixBreadCrumbData;
        $arrmixData['arrmixNetworkSettingsDetails'] = $arrmixNetworkSettingsDetails;
        
        if( $this->request->getPost() ) {
            
            $arrmixRequestData = $this->request->getPost();
            if( false == $this->validation->run( $arrmixRequestData , 'validateSpanningTreeSettings' ) ) { 
                
                $this->session->setFlashdata( 'error', 'Validation Errors. Please check the below errors.' );
                self::setFormValidationErrors( $this->validation->getErrors() );
                
                return $this->render( $arrmixData );
            }

            $arrmixInsertSpanningTreeSettingsData = $arrmixRequestData;
                     
            $intSpanningTreeId = SpanningTreeModel::createService()->add( $arrmixInsertSpanningTreeSettingsData );

            if( true == isValId( $intSpanningTreeId ) ) {
                
                $this->session->setFlashdata( 'success', 'The spanning tree settings has been added successfully.' );

                return redirect()->route( 'spanning-tree/view' );
            } 

            $this->session->setFlashdata( 'error', 'Unable to insert data for com 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( 'basic-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 );

        $arrmixNetworkSettingsDetails = [
            'device_mode'          => $arrmixDeviceConfigJsonFileData['dev_mode'],
            'mode_settings'        => $arrmixDeviceConfigJsonFileData['spt'],
            'mode_settings_status' => $arrmixDeviceConfigJsonFileData['spt_enable']
        ];

        $arrmixSpanningTreeDetails = [
            'priority'       => $arrmixDeviceConfigJsonFileData['spt_conf']['prio'],
            'maximum_age'    => $arrmixDeviceConfigJsonFileData['spt_conf']['max_age'],
            'hello_time'     => $arrmixDeviceConfigJsonFileData['spt_conf']['hello_tm'],
            'forward_delay'  => $arrmixDeviceConfigJsonFileData['spt_conf']['fwrd_delay'],
            'primary_port'   => $arrmixDeviceConfigJsonFileData['spt_conf']['pr_port'],
            'secondary_port' => $arrmixDeviceConfigJsonFileData['spt_conf']['scnd_port'],
        ];

        $arrmixBreadCrumbData['strContentHeader']     = 'Edit Spanning Tree';
        $arrmixBreadCrumbData['arrmixBreadCrumbList'] = [
            ['name' => 'View Spanning Tree', 'is_active' => false, 'href' => getBaseUrl() . 'spanning-tree/view' ],
            ['name' => 'Edit Spanning Tree', 'is_active' => true]
        ];
        
        $arrmixData                 = [];
        $arrmixData['strTitle']     = 'Edit Spanning Tree';
        $arrmixData['strCardTitle'] = 'Edit Spanning Tree';
        $arrmixData['view']         = 'spanning-tree/form-details';

        $arrmixData['arrmixBreadCrumbData']         = $arrmixBreadCrumbData;
        $arrmixData['arrmixSpanningTreeDetails']    = $arrmixSpanningTreeDetails;
        $arrmixData['arrmixNetworkSettingsDetails'] = $arrmixNetworkSettingsDetails;
        $arrmixData['intUsedConfigFile']            = $intUsedConfigFile;
        
        if( true == isValArr( $this->request->getPost() ) ) {
            
            $arrmixRequestData = $this->request->getPost();
            
            if( false == $this->validation->run( $arrmixRequestData , 'validateSpanningTreeSettings' ) ) { 
                
                $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', 'Spanning tree details has been updated successfully.' );
            
            return redirect()->route( 'spanning-tree/view' );        
        }
        
        return $this->render( $arrmixData );
    }    

    public function setConfigFileData( $arrmixUpdateSpanningTreeData ) {
    
        $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['spt_conf'] = [
            'prio'       => $arrmixUpdateSpanningTreeData['priority'],
            'max_age'    => $arrmixUpdateSpanningTreeData['maximum_age'],
            'hello_tm'   => $arrmixUpdateSpanningTreeData['hello_time'],
            'fwrd_delay' => $arrmixUpdateSpanningTreeData['forward_delay'],
            'pr_port'    => $arrmixUpdateSpanningTreeData['primary_port'],
            'scnd_port'  => strtolower( $arrmixUpdateSpanningTreeData['secondary_port'] ),
        ];
        
        $this->g_objServiceLibrary->updateConfigFile( json_encode( $arrmixDeviceConfigJsonFileData ) );

        return true;
    }
}
