/*
  Copyright (c) 2025-2026 Kay Kasper
  under the MIT License (MIT)
*/

#ifndef MAPPING_TESTSWITCH
#define MAPPING_TESTSWITCH

#include "Common.h"

void doSwitchMappingTests(int id){  // ID_SWITCHMAPPINGTEST = 2
  byte mapBuffer[8];
  TestSwitch mapSwitch(8, &mapBuffer[0], INPUT_PIN, 0, 0, true);
  unsigned long tmpMicros, startmicro = 0;
  int seq;

  mapSwitch.setMapping(0, 10);
  mapSwitch.setMapping(1, 11);
  mapSwitch.setMapping(2, 12);
  mapSwitch.setMapping(3, 13);
  mapSwitch.setMapping(4, 14);
  mapSwitch.setMapping(5, 15);
  mapSwitch.setMapping(6, 16);
  mapSwitch.setMapping(7, 17);

  seq = 0; // no waits, negative logic
  mapSwitch.setReadCycleMillis(0);
  mapSwitch.setDebounceMillis(0);
  mapSwitch.setInvertRaw(true);
  mapSwitch.reset();
  check(mapSwitch.getState(),SW_STATE_UNDEFINED,id,seq+1);
  check(mapSwitch.getPrevState(),SW_STATE_UNDEFINED,id,seq+2);
  check(mapSwitch.getMappedState(),SW_STATE_UNDEFINED,id,seq+3);
  check(mapSwitch.getPrevMappedState(),SW_STATE_UNDEFINED,id,seq+4);

  mapSwitch.setRawState(1); // invertRaw -> 6
  check(mapSwitch.getState(),SW_STATE_UNDEFINED,id,seq+5);
  check(mapSwitch.hasChanged(),true,id,seq+6);
  check(mapSwitch.getState(),6,id,seq+7);
  check(mapSwitch.getPrevState(),SW_STATE_UNDEFINED,id,seq+8);
  check(mapSwitch.hasChanged(),false,id,seq+9);
  check(mapSwitch.getMappedState(),16,id,seq+10);
  check(mapSwitch.getPrevMappedState(),SW_STATE_UNDEFINED,id,seq+11);

  mapSwitch.setRawState(0); // invertRaw -> 7
  check(mapSwitch.getState(),6,id,seq+12);
  check(mapSwitch.hasChanged(),true,id,seq+13);
  check(mapSwitch.getState(),7,id,seq+14);
  check(mapSwitch.getPrevState(),6,id,seq+15);
  check(mapSwitch.hasChanged(),false,id,seq+16);
  check(mapSwitch.getMappedState(),17,id,seq+17);
  check(mapSwitch.getPrevMappedState(),16,id,seq+18);

  seq = 20; // no waits, positive logic
  mapSwitch.setReadCycleMillis(0);
  mapSwitch.setDebounceMillis(0);
  mapSwitch.setInvertRaw(false);
  mapSwitch.reset();
  check(mapSwitch.getState(),SW_STATE_UNDEFINED,id,seq+1);
  check(mapSwitch.getPrevState(),SW_STATE_UNDEFINED,id,seq+2);
  check(mapSwitch.getMappedState(),SW_STATE_UNDEFINED,id,seq+3);
  check(mapSwitch.getPrevMappedState(),SW_STATE_UNDEFINED,id,seq+4);

  mapSwitch.setRawState(7);
  check(mapSwitch.getState(),SW_STATE_UNDEFINED,id,seq+5);
  check(mapSwitch.hasChanged(),true,id,seq+6);
  check(mapSwitch.getState(),7,id,seq+7);
  check(mapSwitch.getPrevState(),SW_STATE_UNDEFINED,id,seq+8);
  check(mapSwitch.hasChanged(),false,id,seq+9);
  check(mapSwitch.getMappedState(),17,id,seq+10);
  check(mapSwitch.getPrevMappedState(),SW_STATE_UNDEFINED,id,seq+11);

  mapSwitch.setRawState(3);
  check(mapSwitch.getState(),7,id,seq+12);
  check(mapSwitch.hasChanged(),true,id,seq+13);
  check(mapSwitch.getState(),3,id,seq+14);
  check(mapSwitch.getPrevState(),7,id,seq+15);
  check(mapSwitch.hasChanged(),false,id,seq+16);
  check(mapSwitch.getMappedState(),13,id,seq+17);
  check(mapSwitch.getPrevMappedState(),17,id,seq+18);

  // performance

  Serial.println("\nPerformance Mapping:");

  mapSwitch.setInvertRaw(false);
  mapSwitch.setReadCycleMillis(0);
  mapSwitch.setDebounceMillis(0);
  mapSwitch.reset();

  Serial.print("1000 * hasChanged(true) + getState(): ");
  startmicro = micros();
  for(int i = 0 ; i < 1000 ; i++){
    mapSwitch.setRawState(i & 0x0007);
    mapSwitch.hasChanged();
    if(mapSwitch.getState() > 200) break;
  }
  Serial.print(micros() - startmicro);
  Serial.println(" micros");

  Serial.print("1000 * hasChanged(true) + getMappedState(): ");
  startmicro = micros();
  for(int i = 0 ; i < 1000 ; i++){
    mapSwitch.setRawState(i & 0x0007);
    mapSwitch.hasChanged();
    if(mapSwitch.getMappedState() > 200) break;
  }
  Serial.print(micros() - startmicro);
  Serial.println(" micros");

  Serial.print("1000 * hasChanged(true) + getPrevState(): ");
  startmicro = micros();
  for(int i = 0 ; i < 1000 ; i++){
    mapSwitch.setRawState(i & 0x0007);
    mapSwitch.hasChanged();
    if(mapSwitch.getPrevState() > 200) break;
  }
  Serial.print(micros() - startmicro);
  Serial.println(" micros");

  Serial.print("1000 * hasChanged(true) + getPrevMappedState(): ");
  startmicro = micros();
  for(int i = 0 ; i < 1000 ; i++){
    mapSwitch.setRawState(i & 0x0007);
    mapSwitch.hasChanged();
    if(mapSwitch.getPrevMappedState() > 200) break;
  }
  Serial.print(micros() - startmicro);
  Serial.println(" micros");
}

#endif
