using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Linq;
using Crestron;
using Crestron.Logos.SplusLibrary;
using Crestron.Logos.SplusObjects;
using Crestron.SimplSharp;

namespace UserModule_BG_MVS4X4_4KL
{
    public class UserModuleClass_BG_MVS4X4_4KL : SplusObject
    {
        static CCriticalSection g_criticalSection = new CCriticalSection();
        
        
        Crestron.Logos.SplusObjects.DigitalInput POWER_ON;
        Crestron.Logos.SplusObjects.DigitalInput POWER_OFF;
        Crestron.Logos.SplusObjects.BufferInput RX_MATRIX;
        Crestron.Logos.SplusObjects.AnalogInput STORE_PRESET_MODE;
        Crestron.Logos.SplusObjects.AnalogInput RECALL_PRESET_MODE;
        Crestron.Logos.SplusObjects.AnalogInput VIDEOWALL_MODE;
        InOutArray<Crestron.Logos.SplusObjects.AnalogInput> OUTPUT;
        Crestron.Logos.SplusObjects.StringOutput TX_MATRIX;
        Crestron.Logos.SplusObjects.StringOutput TO_SIO;
        object POWER_ON_OnPush_0 ( Object __EventInfo__ )
        
            { 
            Crestron.Logos.SplusObjects.SignalEventArgs __SignalEventArg__ = (Crestron.Logos.SplusObjects.SignalEventArgs)__EventInfo__;
            try
            {
                SplusExecutionContext __context__ = SplusThreadStartCode(__SignalEventArg__);
                
                __context__.SourceCodeLine = 158;
                TX_MATRIX  .UpdateValue ( "s power 1!"  ) ; 
                
                
            }
            catch(Exception e) { ObjectCatchHandler(e); }
            finally { ObjectFinallyHandler( __SignalEventArg__ ); }
            return this;
            
        }
        
    object POWER_OFF_OnPush_1 ( Object __EventInfo__ )
    
        { 
        Crestron.Logos.SplusObjects.SignalEventArgs __SignalEventArg__ = (Crestron.Logos.SplusObjects.SignalEventArgs)__EventInfo__;
        try
        {
            SplusExecutionContext __context__ = SplusThreadStartCode(__SignalEventArg__);
            
            __context__.SourceCodeLine = 165;
            TX_MATRIX  .UpdateValue ( "s power 0!"  ) ; 
            
            
        }
        catch(Exception e) { ObjectCatchHandler(e); }
        finally { ObjectFinallyHandler( __SignalEventArg__ ); }
        return this;
        
    }
    
object RX_MATRIX_OnChange_2 ( Object __EventInfo__ )

    { 
    Crestron.Logos.SplusObjects.SignalEventArgs __SignalEventArg__ = (Crestron.Logos.SplusObjects.SignalEventArgs)__EventInfo__;
    try
    {
        SplusExecutionContext __context__ = SplusThreadStartCode(__SignalEventArg__);
        CrestronString MESSAGE;
        MESSAGE  = new CrestronString( Crestron.Logos.SplusObjects.CrestronStringEncoding.eEncodingASCII, 65534, this );
        
        CrestronString INFO;
        INFO  = new CrestronString( Crestron.Logos.SplusObjects.CrestronStringEncoding.eEncodingASCII, 65534, this );
        
        ushort P1 = 0;
        ushort P2 = 0;
        ushort P3 = 0;
        ushort P4 = 0;
        
        
        __context__.SourceCodeLine = 182;
        MESSAGE  .UpdateValue ( MESSAGE + RX_MATRIX  ) ; 
        __context__.SourceCodeLine = 183;
        Functions.Delay (  (int) ( 100 ) ) ; 
        __context__.SourceCodeLine = 184;
        Functions.Delay (  (int) ( 100 ) ) ; 
        __context__.SourceCodeLine = 185;
        P1 = (ushort) ( Functions.Find( "Output" , MESSAGE ) ) ; 
        __context__.SourceCodeLine = 186;
        INFO  .UpdateValue ( Functions.Mid ( MESSAGE ,  (int) ( P1 ) ,  (int) ( 100 ) )  ) ; 
        __context__.SourceCodeLine = 188;
        TO_SIO  .UpdateValue ( INFO  ) ; 
        __context__.SourceCodeLine = 190;
        Functions.ClearBuffer ( RX_MATRIX ) ; 
        
        
    }
    catch(Exception e) { ObjectCatchHandler(e); }
    finally { ObjectFinallyHandler( __SignalEventArg__ ); }
    return this;
    
}

object STORE_PRESET_MODE_OnChange_3 ( Object __EventInfo__ )

    { 
    Crestron.Logos.SplusObjects.SignalEventArgs __SignalEventArg__ = (Crestron.Logos.SplusObjects.SignalEventArgs)__EventInfo__;
    try
    {
        SplusExecutionContext __context__ = SplusThreadStartCode(__SignalEventArg__);
        CrestronString STRINGOUT1;
        STRINGOUT1  = new CrestronString( Crestron.Logos.SplusObjects.CrestronStringEncoding.eEncodingASCII, 2, this );
        
        
        __context__.SourceCodeLine = 201;
        MakeString ( STRINGOUT1 , "{0:d}", (ushort)STORE_PRESET_MODE  .UshortValue) ; 
        __context__.SourceCodeLine = 202;
        TX_MATRIX  .UpdateValue ( "s save preset " + STRINGOUT1 + "!"  ) ; 
        
        
    }
    catch(Exception e) { ObjectCatchHandler(e); }
    finally { ObjectFinallyHandler( __SignalEventArg__ ); }
    return this;
    
}

object RECALL_PRESET_MODE_OnChange_4 ( Object __EventInfo__ )

    { 
    Crestron.Logos.SplusObjects.SignalEventArgs __SignalEventArg__ = (Crestron.Logos.SplusObjects.SignalEventArgs)__EventInfo__;
    try
    {
        SplusExecutionContext __context__ = SplusThreadStartCode(__SignalEventArg__);
        CrestronString STRINGOUT1;
        STRINGOUT1  = new CrestronString( Crestron.Logos.SplusObjects.CrestronStringEncoding.eEncodingASCII, 2, this );
        
        
        __context__.SourceCodeLine = 212;
        MakeString ( STRINGOUT1 , "{0:d}", (ushort)RECALL_PRESET_MODE  .UshortValue) ; 
        __context__.SourceCodeLine = 213;
        TX_MATRIX  .UpdateValue ( "s recall preset " + STRINGOUT1 + "!"  ) ; 
        
        
    }
    catch(Exception e) { ObjectCatchHandler(e); }
    finally { ObjectFinallyHandler( __SignalEventArg__ ); }
    return this;
    
}

object OUTPUT_OnChange_5 ( Object __EventInfo__ )

    { 
    Crestron.Logos.SplusObjects.SignalEventArgs __SignalEventArg__ = (Crestron.Logos.SplusObjects.SignalEventArgs)__EventInfo__;
    try
    {
        SplusExecutionContext __context__ = SplusThreadStartCode(__SignalEventArg__);
        ushort LASTNUM = 0;
        
        CrestronString SINPUT;
        CrestronString SOUTPUT;
        SINPUT  = new CrestronString( Crestron.Logos.SplusObjects.CrestronStringEncoding.eEncodingASCII, 1, this );
        SOUTPUT  = new CrestronString( Crestron.Logos.SplusObjects.CrestronStringEncoding.eEncodingASCII, 1, this );
        
        
        __context__.SourceCodeLine = 221;
        LASTNUM = (ushort) ( Functions.GetLastModifiedArrayIndex( __SignalEventArg__ ) ) ; 
        __context__.SourceCodeLine = 222;
        if ( Functions.TestForTrue  ( ( Functions.BoolToInt (LASTNUM == 1))  ) ) 
            {
            __context__.SourceCodeLine = 222;
            SOUTPUT  .UpdateValue ( "1"  ) ; 
            }
        
        else 
            {
            __context__.SourceCodeLine = 223;
            SOUTPUT  .UpdateValue ( Functions.ItoA (  (int) ( LASTNUM ) )  ) ; 
            }
        
        __context__.SourceCodeLine = 224;
        if ( Functions.TestForTrue  ( ( Functions.BoolToInt ( OUTPUT[ LASTNUM ] .UshortValue < 1 ))  ) ) 
            {
            __context__.SourceCodeLine = 224;
            SINPUT  .UpdateValue ( "0" + Functions.ItoA (  (int) ( OUTPUT[ LASTNUM ] .UshortValue ) )  ) ; 
            }
        
        else 
            {
            __context__.SourceCodeLine = 225;
            if ( Functions.TestForTrue  ( ( Functions.BoolToInt (OUTPUT[ LASTNUM ] .UshortValue == 9))  ) ) 
                {
                __context__.SourceCodeLine = 225;
                SINPUT  .UpdateValue ( "0"  ) ; 
                }
            
            else 
                {
                __context__.SourceCodeLine = 226;
                SINPUT  .UpdateValue ( Functions.ItoA (  (int) ( OUTPUT[ LASTNUM ] .UshortValue ) )  ) ; 
                }
            
            }
        
        __context__.SourceCodeLine = 227;
        MakeString ( TX_MATRIX , "s in {0} out {1}!", SINPUT , SOUTPUT ) ; 
        
        
    }
    catch(Exception e) { ObjectCatchHandler(e); }
    finally { ObjectFinallyHandler( __SignalEventArg__ ); }
    return this;
    
}

object VIDEOWALL_MODE_OnChange_6 ( Object __EventInfo__ )

    { 
    Crestron.Logos.SplusObjects.SignalEventArgs __SignalEventArg__ = (Crestron.Logos.SplusObjects.SignalEventArgs)__EventInfo__;
    try
    {
        SplusExecutionContext __context__ = SplusThreadStartCode(__SignalEventArg__);
        CrestronString STRINGOUT1;
        STRINGOUT1  = new CrestronString( Crestron.Logos.SplusObjects.CrestronStringEncoding.eEncodingASCII, 2, this );
        
        
        __context__.SourceCodeLine = 238;
        MakeString ( STRINGOUT1 , "{0:d}", (ushort)VIDEOWALL_MODE  .UshortValue) ; 
        __context__.SourceCodeLine = 239;
        TX_MATRIX  .UpdateValue ( "s tw mode " + STRINGOUT1 + "!"  ) ; 
        
        
    }
    catch(Exception e) { ObjectCatchHandler(e); }
    finally { ObjectFinallyHandler( __SignalEventArg__ ); }
    return this;
    
}


public override void LogosSplusInitialize()
{
    SocketInfo __socketinfo__ = new SocketInfo( 1, this );
    InitialParametersClass.ResolveHostName = __socketinfo__.ResolveHostName;
    _SplusNVRAM = new SplusNVRAM( this );
    
    POWER_ON = new Crestron.Logos.SplusObjects.DigitalInput( POWER_ON__DigitalInput__, this );
    m_DigitalInputList.Add( POWER_ON__DigitalInput__, POWER_ON );
    
    POWER_OFF = new Crestron.Logos.SplusObjects.DigitalInput( POWER_OFF__DigitalInput__, this );
    m_DigitalInputList.Add( POWER_OFF__DigitalInput__, POWER_OFF );
    
    STORE_PRESET_MODE = new Crestron.Logos.SplusObjects.AnalogInput( STORE_PRESET_MODE__AnalogSerialInput__, this );
    m_AnalogInputList.Add( STORE_PRESET_MODE__AnalogSerialInput__, STORE_PRESET_MODE );
    
    RECALL_PRESET_MODE = new Crestron.Logos.SplusObjects.AnalogInput( RECALL_PRESET_MODE__AnalogSerialInput__, this );
    m_AnalogInputList.Add( RECALL_PRESET_MODE__AnalogSerialInput__, RECALL_PRESET_MODE );
    
    VIDEOWALL_MODE = new Crestron.Logos.SplusObjects.AnalogInput( VIDEOWALL_MODE__AnalogSerialInput__, this );
    m_AnalogInputList.Add( VIDEOWALL_MODE__AnalogSerialInput__, VIDEOWALL_MODE );
    
    OUTPUT = new InOutArray<AnalogInput>( 4, this );
    for( uint i = 0; i < 4; i++ )
    {
        OUTPUT[i+1] = new Crestron.Logos.SplusObjects.AnalogInput( OUTPUT__AnalogSerialInput__ + i, OUTPUT__AnalogSerialInput__, this );
        m_AnalogInputList.Add( OUTPUT__AnalogSerialInput__ + i, OUTPUT[i+1] );
    }
    
    TX_MATRIX = new Crestron.Logos.SplusObjects.StringOutput( TX_MATRIX__AnalogSerialOutput__, this );
    m_StringOutputList.Add( TX_MATRIX__AnalogSerialOutput__, TX_MATRIX );
    
    TO_SIO = new Crestron.Logos.SplusObjects.StringOutput( TO_SIO__AnalogSerialOutput__, this );
    m_StringOutputList.Add( TO_SIO__AnalogSerialOutput__, TO_SIO );
    
    RX_MATRIX = new Crestron.Logos.SplusObjects.BufferInput( RX_MATRIX__AnalogSerialInput__, 255, this );
    m_StringInputList.Add( RX_MATRIX__AnalogSerialInput__, RX_MATRIX );
    
    
    POWER_ON.OnDigitalPush.Add( new InputChangeHandlerWrapper( POWER_ON_OnPush_0, false ) );
    POWER_OFF.OnDigitalPush.Add( new InputChangeHandlerWrapper( POWER_OFF_OnPush_1, false ) );
    RX_MATRIX.OnSerialChange.Add( new InputChangeHandlerWrapper( RX_MATRIX_OnChange_2, false ) );
    STORE_PRESET_MODE.OnAnalogChange.Add( new InputChangeHandlerWrapper( STORE_PRESET_MODE_OnChange_3, false ) );
    RECALL_PRESET_MODE.OnAnalogChange.Add( new InputChangeHandlerWrapper( RECALL_PRESET_MODE_OnChange_4, false ) );
    for( uint i = 0; i < 4; i++ )
        OUTPUT[i+1].OnAnalogChange.Add( new InputChangeHandlerWrapper( OUTPUT_OnChange_5, false ) );
        
    VIDEOWALL_MODE.OnAnalogChange.Add( new InputChangeHandlerWrapper( VIDEOWALL_MODE_OnChange_6, false ) );
    
    _SplusNVRAM.PopulateCustomAttributeList( true );
    
    NVRAM = _SplusNVRAM;
    
}

public override void LogosSimplSharpInitialize()
{
    
    
}

public UserModuleClass_BG_MVS4X4_4KL ( string InstanceName, string ReferenceID, Crestron.Logos.SplusObjects.CrestronStringEncoding nEncodingType ) : base( InstanceName, ReferenceID, nEncodingType ) {}




const uint POWER_ON__DigitalInput__ = 0;
const uint POWER_OFF__DigitalInput__ = 1;
const uint RX_MATRIX__AnalogSerialInput__ = 0;
const uint STORE_PRESET_MODE__AnalogSerialInput__ = 1;
const uint RECALL_PRESET_MODE__AnalogSerialInput__ = 2;
const uint VIDEOWALL_MODE__AnalogSerialInput__ = 3;
const uint OUTPUT__AnalogSerialInput__ = 4;
const uint TX_MATRIX__AnalogSerialOutput__ = 0;
const uint TO_SIO__AnalogSerialOutput__ = 1;

[SplusStructAttribute(-1, true, false)]
public class SplusNVRAM : SplusStructureBase
{

    public SplusNVRAM( SplusObject __caller__ ) : base( __caller__ ) {}
    
    
}

SplusNVRAM _SplusNVRAM = null;

public class __CEvent__ : CEvent
{
    public __CEvent__() {}
    public void Close() { base.Close(); }
    public int Reset() { return base.Reset() ? 1 : 0; }
    public int Set() { return base.Set() ? 1 : 0; }
    public int Wait( int timeOutInMs ) { return base.Wait( timeOutInMs ) ? 1 : 0; }
}
public class __CMutex__ : CMutex
{
    public __CMutex__() {}
    public void Close() { base.Close(); }
    public void ReleaseMutex() { base.ReleaseMutex(); }
    public int WaitForMutex() { return base.WaitForMutex() ? 1 : 0; }
}
 public int IsNull( object obj ){ return (obj == null) ? 1 : 0; }
}


}
