using System;
using System.Threading;

namespace Raytheon.GuidedElectronicsUnit
{
    internal class HardwareSession6020a : HardwareSession
        , IGuidedElectronicsUnitLowLevelHSSub6020a
        , IGuidedElectronicsUnitIntegratedDewarAssemblyInterfaceEmulator
    {
        public HardwareSession6020a()
        {
            ApplicationConstant = 0x4C01;
            ApplicationPrefix = "GEU";
            ChipId = "L1";
            AdcBitCount = 10;

            WordSwap = false;

            ExpectedApplicationId = RegisterMapHSSub6020a.ExpectedAppID;
            ExpectedCustomerId = RegisterMapHSSub6020a.ExpectedCustID;
            ExpectedRevisionId = RegisterMapHSSub6020a.ExpectedRevID;

            RegisterConstant = RegisterMapHSSub6020a.TER_LB__FPGA_CONST;
            RegisterApplicationCustomerId = RegisterMapHSSub6020a.TER_LB__APP_CUST_ID;
            RegisterRevisionId = RegisterMapHSSub6020a.TER_LB__REV_ID;
            RegisterPllStatus = RegisterMapHSSub6020a.TER_LB__PLL;
            RegisterVccIntDieTemp = RegisterMapHSSub6020a.TER_LB__VCCINT_DT;
            RegisterVccAux = RegisterMapHSSub6020a.TER_LB__VCCAUX;
        }

        public IGuidedElectronicsUnitIntegratedDewarAssemblyInterfaceEmulator IdaEmulator => this;
        public IGuidedElectronicsUnitLowLevelHSSub6020a LowLevel => this;

        #region Protected Methods

        protected override void GetInitialDefaults()
        {
            base.GetInitialDefaults();
        }

        protected override void SetDefaults()
        {
            base.SetDefaults();

            IdaEmulator.Control = ControlState.Disabled;
        }

        #endregion

        #region IdaEmulator

        ControlState IGuidedElectronicsUnitIntegratedDewarAssemblyInterfaceEmulator.Control
        {
            get
            {
                return IsBitSet(ReadRegister(RegisterMapHSSub6020a.EM_CONTROL), 0) ? ControlState.Enabled : ControlState.Disabled;
            }
            set
            {
                ReadModifyWrite(RegisterMapHSSub6020a.EM_CONTROL, Convert.ToUInt32(value), 0x1);
            }
        }

        IdaStatus IGuidedElectronicsUnitIntegratedDewarAssemblyInterfaceEmulator.Status
        {
            get
            {
                return new(ReadRegister(RegisterMapHSSub6020a.EM_STATUS));
            }
        }

        uint IGuidedElectronicsUnitIntegratedDewarAssemblyInterfaceEmulator.Version
        {
            get
            {
                return ReadRegister(RegisterMapHSSub6020a.II_EM_VERSION);
            }
        }

        #endregion

        #region LowLevel

        bool IGuidedElectronicsUnitLowLevelHSSub6020a.IsSimulated => IsSimulated;

        int IGuidedElectronicsUnitLowLevelHSSub6020a.Chassis => Chassis;

        string IGuidedElectronicsUnitLowLevelHSSub6020a.Descriptor => Descriptor;

        string IGuidedElectronicsUnitLowLevelHSSub6020a.SerialNumber => SerialNumber;

        int IGuidedElectronicsUnitLowLevelHSSub6020a.Slot => Slot;

        void IGuidedElectronicsUnitLowLevelHSSub6020a.Reset() => Reset();

        FirmwareInformation IGuidedElectronicsUnitLowLevelHSSub6020a.GetFirmwareInfo() => GetFirmwareInfo();

        TdfPllStatus IGuidedElectronicsUnitLowLevelHSSub6020a.GetPllStatus() => GetPllStatus();

        uint IGuidedElectronicsUnitLowLevelHSSub6020a.ReadRegister(uint address) =>
            ReadRegister(address);

        void IGuidedElectronicsUnitLowLevelHSSub6020a.ReadModifyWrite(uint address, uint data, uint mask) =>
            ReadModifyWrite(address, data, mask);

        void IGuidedElectronicsUnitLowLevelHSSub6020a.WriteRegister(uint address, uint data) =>
            WriteRegister(address, data);

        double IGuidedElectronicsUnitLowLevelHSSub6020a.DieTemp => DieTemp;

        double IGuidedElectronicsUnitLowLevelHSSub6020a.VccInt => VccInt;

        double IGuidedElectronicsUnitLowLevelHSSub6020a.VccAux => VccAux;


        ControlState IGuidedElectronicsUnitLowLevelHSSub6020a.EnableIO
        {
            get
            {
                CallLoggingHelper.FunctionStart(GetType(), "getEnableIO");

                ControlState state = ControlState.Disabled;
                try
                {
                    state = IsBitSet(ReadRegister(RegisterMapHSSub6020a.FP_IO_CTRL), 0) ? ControlState.Enabled : ControlState.Disabled;
                    return state;
                }
                finally
                {
                    CallLoggingHelper.FunctionEnd(GetType(), "getEnableIO", state);
                }
            }
            set
            {
                CallLoggingHelper.FunctionStart(GetType(), "setEnableIO", value);

                try
                {
                    WriteRegister(RegisterMapHSSub6020a.FP_IO_CTRL, (uint)(Convert.ToBoolean(value) ? 0 : 1));
                }
                finally
                {
                    CallLoggingHelper.FunctionEnd(GetType(), "setEnableIO", null);
                }
            }
        }

        EmStatus IGuidedElectronicsUnitLowLevelHSSub6020a.EmStatus
        {
            get => new(ReadRegister(RegisterMapHSSub6020a.EM_STATUS));
        }

        void IGuidedElectronicsUnitLowLevelHSSub6020a.ResetEmStatus()
        {
            WriteRegister(RegisterMapHSSub6020a.EM_STATUS, 0x0);
            Thread.Sleep(1000);
        }

        #endregion

    }
}
