import time
import json
from itertools import repeat


example_response = """{
    "TimeCycle": {
        "Cycle": 0,
        "OverallCycle": 0,
        "AbsTime": 0,
        "RelTime": 0
    },
    "raw": [],
    "corr": [],
    "conc": [],
    "CalcTraces": [],
    "AddData": [
        {
            "Name": "TOFSupply",
            "Desc": [
                "TPS_Lens1_Set",
                "TPS_Lens1_Act",
                "TPS_Lens2_Set",
                "TPS_Lens2_Act",
                "TPS_Lens3_Set",
                "TPS_Lens3_Act",
                "TPS_Lens4_Set",
                "TPS_Lens4_Act",
                "TPS_Lens5_Set",
                "TPS_Lens5_Act",
                "TPS_Lens6_Set",
                "TPS_Lens6_Act",
                "TPS_Lens7_Set",
                "TPS_Lens7_Act",
                "TPS_LensesEn_Act",
                "TPS_Push_L_Set",
                "TPS_Push_L_Act",
                "TPS_Push_H_Set",
                "TPS_Push_H_Act",
                "TPS_Pull_L_Set",
                "TPS_Pull_L_Act",
                "TPS_Pull_H_Set",
                "TPS_Pull_H_Act",
                "TPS_Push_L_En_Act",
                "TPS_Push_H_En_Act",
                "TPS_Pull_L_En_Act",
                "TPS_Pull_H_En_Act",
                "TPS_I-Push L_Act",
                "TPS_I-Push H_Act",
                "TPS_I-Pull L_Act",
                "TPS_I-Pull H_Act",
                "TPS_Grid_Set",
                "TPS_Grid_Act",
                "TPS_Cage_Set",
                "TPS_Cage_Act",
                "TPS_Refl_Grid_Set",
                "TPS_Refl_Grid_Act",
                "TPS_Refl_Back_Set",
                "TPS_Refl_Back_Act",
                "TPS_MCP_F_Set",
                "TPS_MCP_F_Act",
                "TPS_MCP_B_Set",
                "TPS_MCP_B_Act",
                "TPS_Grid_En_Act",
                "TPS_Cage_En_Act",
                "TPS_ReflGrid_En_Act",
                "TPS_ReflBack_En_Act",
                "TPS_MCP_F_En_Act",
                "TPS_MCP_B_En_Act",
                "TPS_I-Grid_Act",
                "TPS_I-Cage_Act",
                "TPS_I-Refl. Grid_Act",
                "TPS_I-Refl. Back_Act",
                "TPS_I-MCP F_Act",
                "TPS_I-MCP B_Act",
                "TPS_Hex1Offset_Set",
                "TPS_Hex1Offset_Act",
                "TPS_Hex1Enabled_Set",
                "TPS_Hex1Enabled_Act",
                "TPS_Hex1Ampl_Set",
                "TPS_Hex1Ampl_Act"
            ],
            "Units": [
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "",
                "",
                "",
                "",
                "mA",
                "mA",
                "mA",
                "mA",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "",
                "",
                "",
                "",
                "",
                "",
                "\u00c2\u00b5A",
                "\u00c2\u00b5A",
                "\u00c2\u00b5A",
                "\u00c2\u00b5A",
                "\u00c2\u00b5A",
                "\u00c2\u00b5A",
                "V",
                "V",
                "",
                "",
                "mA",
                "mA"
            ],
            "Data": [
                100,
                99,
                45,
                45,
                109.99998474121094,
                109,
                192,
                190,
                52,
                52,
                26,
                26,
                50,
                50,
                1,
                37.000099182128906,
                37,
                720,
                719,
                50,
                50,
                600,
                600,
                1,
                1,
                1,
                1,
                0,
                0,
                0,
                0,
                1700,
                1614,
                3000,
                2846,
                690,
                655,
                808.999755859375,
                768,
                5300,
                5032,
                1950.402099609375,
                1965,
                1,
                1,
                1,
                1,
                1,
                1,
                6,
                71,
                100,
                171,
                17,
                123,
                0,
                0,
                0,
                0,
                0,
                0
            ]
        },
        {
            "Name": "DataCollection",
            "Desc": [
                "ACQ_SRV_SpecTime_ms_Set",
                "ACQ_SRV_MassCal_a_Act",
                "ACQ_SRV_MassCal_b_Act",
                "ACQ_SRV_AutoCalOnOff_Set",
                "ACQ_SRV_AutoCalPeriod_sec_Set"
            ],
            "Units": [
                " ",
                " ",
                " ",
                "",
                ""
            ],
            "Data": [
                1000,
                6929.77099609375,
                1569.69482421875,
                1,
                20
            ]
        },
        {
            "Name": "PTR-Reaction",
            "Desc": [
                "Udrift_Act",
                "p-Drift_Act",
                "T-Drift_Act",
                "E/N_Act",
                "PrimIon_Act",
                "TransSet_Act"
            ],
            "Units": [
                "V",
                "mbar",
                "\u00c2\u00b0C",
                "Td",
                "Idx",
                "Idx"
            ],
            "Data": [
                480.867431640625,
                2.305406332015991,
                70.19999694824219,
                139.5508575439453,
                0,
                0
            ]
        },
        {
            "Name": "PTR-Instrument",
            "Desc": [
                "Us_Set",
                "Us_Act",
                "Uso_Set",
                "Uso_Act",
                "Udrift_Set",
                "Udrift_Act",
                "Udx_Set",
                "Udx_Act",
                "Ihc_Set",
                "Ihc_Act",
                "Source On/Off_Set",
                "Source On/Off_Act",
                "PrimIon_Set",
                "PrimIon_Act",
                "SV_Set",
                "SV_Act",
                "E/N_Set",
                "E/N_Act",
                "p-Foreline_Set",
                "p-Foreline_Act",
                "p-Drift_Set",
                "p-Drift_Act",
                "p-TofLens_Set",
                "p-TofLens_Act",
                "p-TOF_Set",
                "p-TOF_Act",
                "H2O_Set",
                "H2O_Act",
                "PC_Set",
                "PC_Act",
                "FCinlet_Set",
                "FCinlet_Act",
                "NO_Set",
                "NO_Act",
                "Preset_Act",
                "Temp: T-Drift_Set",
                "Temp: T-Drift_Act",
                "Temp: T-Inlet_Set",
                "Temp: T-Inlet_Act",
                "Temp: T-Opt1_Set",
                "Temp: T-Opt1_Act",
                "Temp: PerMasCal_Set",
                "Temp: PerMasCal_Act",
                "Drift_Set",
                "Drift_Act",
                "Drift_Set",
                "Drift_Act",
                "Drift: Telectronics_Set",
                "Drift: Telectronics_Act",
                "Drift: Tmotor_Set",
                "Drift: Tmotor_Act",
                "Detection_Set",
                "Detection_Act",
                "Detection_Set",
                "Detection_Act",
                "Detection: Telectronics_Set",
                "Detection: Telectronics_Act",
                "Detection: Tmotor_Set",
                "Detection: Tmotor_Act",
                "HS_Set",
                "HS_Act",
                "HS_Set",
                "HS_Act",
                "HS: Telectronics_Set",
                "HS: Telectronics_Act",
                "HS: Tmotor_Set",
                "HS: Tmotor_Act",
                "DPS_Pdrift_Ctrl_OnOff_Set",
                "DPS_Pdrift_Ctrl_OnOff_Act",
                "DPS_Pdrift_Ctrl_Val_Set",
                "DPS_Pdrift_Ctrl_Val_Act"
            ],
            "Units": [
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "V",
                "mA",
                "mA",
                "",
                "",
                "Idx",
                "Idx",
                "%",
                "%",
                "Td",
                "Td",
                "mbar",
                "mbar",
                "mbar",
                "mbar",
                "mbar",
                "mbar",
                "mbar",
                "mbar",
                "sccm   ",
                "sccm   ",
                "mbar(a)",
                "mbar(a)",
                "sccm   ",
                "sccm   ",
                "sccm   ",
                "sccm   ",
                "Idx",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "Hz",
                "Hz",
                "A",
                "A",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "Hz",
                "Hz",
                "A",
                "A",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "Hz",
                "Hz",
                "A",
                "A",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "\u00c2\u00b0C",
                "0/1",
                "0/1",
                "mbar",
                "mbar"
            ],
            "Data": [
                140,
                135.4322509765625,
                80,
                78.55672454833984,
                480,
                480.867431640625,
                34,
                32.611846923828125,
                4,
                4.023482799530029,
                1,
                1,
                0,
                0,
                59,
                59,
                139.5508575439453,
                139.5508575439453,
                0,
                4.874532699584961,
                0,
                2.305406332015991,
                0,
                3.095457941526547e-05,
                0,
                7.551057592536381e-07,
                6,
                6.000076770782471,
                246.40625,
                246.4048614501953,
                0,
                0,
                0,
                0,
                110,
                70,
                70.19999694824219,
                80,
                850,
                60,
                850,
                60,
                59.900001525878906,
                900,
                900,
                NaN,
                0.9300000071525574,
                NaN,
                52,
                NaN,
                50,
                1500,
                1500,
                NaN,
                0.7300000190734863,
                NaN,
                45,
                NaN,
                45,
                1500,
                1500,
                NaN,
                0.6600000262260437,
                NaN,
                48,
                NaN,
                43,
                1,
                1,
                2.299999952316284,
                2.3050248622894287
            ]
        },
        {
            "Name": "PTR-Misc",
            "Desc": [
                "AI1_Act",
                "AI2_Act",
                "DI1_Act",
                "DI2_Act",
                "AO1_Set",
                "AO2_Set",
                "DO1_Set",
                "DO2_Set",
                "DO3_Set",
                "DO4_Set"
            ],
            "Units": [
                "",
                "",
                "",
                "",
                "V",
                "V",
                "",
                "",
                "",
                ""
            ],
            "Data": [
                0.001831110566854477,
                0.0006103701889514923,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0
            ]
        },
        {
            "Name": "Automation",
            "Desc": [
                "AUTO_StepNumber",
                "AUTO_RunNumber",
                "AUTO_UseMean",
                "AUTO_StartCycleMean",
                "AUTO_StopCycleMean",
                "AME_ActionNumber",
                "AME_UserNumber",
                "AME_StepNumber",
                "AME_RunNumber"
            ],
            "Units": [
                "Number",
                "Number",
                "Number",
                "Number",
                "Number",
                "Number",
                "Number",
                "Number",
                "Number"
            ],
            "Data": [
                1,
                552,
                1,
                5213,
                5541,
                3,
                0,
                1,
                552
            ]
        }
    ],
    "CalcTracesNames": [],
    "masses": []
}"""

class MockClient:
    '''
    Mock out all server connections for testing.
    '''
    def __init__(self, host='localhost', port=8002):
        self.host = host
        self.port = port
        self.running = False
        self.cycle = 0

    @property
    def baseurl(self):
        return f'http://{self.host}:{self.port}/Ic_WebAPI'

    def get(self, varname):
        return "{" + varname + ": 42}"

    def get_many(self, varnames):
        mock = dict(zip(varnames, repeat(42)))
        return str(mock)

    def get_traces(self):
        if self.running:
            self.cycle += 1

        response = json.loads(example_response)
        response['TimeCycle']['Cycle'] = self.cycle 
        response['TimeCycle']['OverallCycle'] = self.cycle 

        return json.dumps(response)

    def set(self, varname, value):
        return "kruxlwuxl"

    def set_many(self, key_value_pairs):
        return "kruxlwuxl"

    def set_filename(self, filename):
        return "kruxlwuxl"

    def start_measurement(self, filename=''):
        time.sleep(1.2)
        self.running = True
        return "kruxlwuxl"

    def stop_measurement(self):
        time.sleep(1.2)
        self.running = False
        return "kruxlwuxl"

