import Keyboard from '../../components/Keyboard';
import TestUtility from '../../../utils/TestUtility';

const testUtil = new TestUtility();

it('Keyboard mergeDisplay will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    mergeDisplay: true,
    display: {
      "q": "qreplaced"
    }
  });
  
  expect(keyboard.getButtonElement("q").querySelector("span").innerHTML).toBe("qreplaced");
});

it('Keyboard function buttons will work', () => {
  testUtil.setDOM();

  new Keyboard();
  
  testUtil.testLayoutFctButtons((fctBtnCount, fctBtnHasOnclickCount) => {
    expect(fctBtnCount).toBe(fctBtnHasOnclickCount);
  });
});

it('Keyboard {bksp} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{bksp}", "test");
  
  expect(output).toBe("tes");
});

it('Keyboard {space} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{space}", "test");
  
  expect(output).toBe("test ");
});

it('Keyboard {tab} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{tab}", "test");
  
  expect(output).toBe("test\t");
});

it('Keyboard {tab} button will work with tabCharOnTab:false', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    tabCharOnTab: false
  });

  const output = keyboard.utilities.getUpdatedInput("{tab}", "test");
  
  expect(output).toBe("test");
});

it('Keyboard {enter} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{enter}", "test");
  
  expect(output).toBe("test");
});

it('Keyboard {enter} button will work with newLineOnEnter:true', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    newLineOnEnter: true
  });

  const output = keyboard.utilities.getUpdatedInput("{enter}", "test");
  
  expect(output).toBe("test\n");
});

it('Keyboard {numpadX} buttons will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  for(let i = 0;i<=9;i++){
    const output = keyboard.utilities.getUpdatedInput(`{numpad${i}}`, "test");
    expect(output).toBe(`test${i}`);
  }
});

it('Keyboard {numpaddivide} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{numpaddivide}", "test");
  
  expect(output).toBe("test/");
});

it('Keyboard {numpadmultiply} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{numpadmultiply}", "test");
  
  expect(output).toBe("test*");
});

it('Keyboard {numpadsubtract} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{numpadsubtract}", "test");
  
  expect(output).toBe("test-");
});

it('Keyboard {numpadadd} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{numpadadd}", "test");
  
  expect(output).toBe("test+");
});

it('Keyboard {numpadadd} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{numpadadd}", "test");
  
  expect(output).toBe("test+");
});

it('Keyboard {numpaddecimal} button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{numpaddecimal}", "test");
  
  expect(output).toBe("test.");
});

it('Keyboard custom function buttons will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    layout: {
      default: [
        "{randombuttontest}"
      ]
    }
  });

  const output = keyboard.utilities.getUpdatedInput("{randombuttontest}", "test");
  
  expect(output).toBe("test");
  expect(keyboard.getButtonElement("{randombuttontest}").onclick).toBeTruthy();
});

it('Keyboard "{" button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("{", "test");
  
  expect(output).toBe("test{");
});

it('Keyboard "}" button will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  const output = keyboard.utilities.getUpdatedInput("}", "test");
  
  expect(output).toBe("test}");
});

it('Keyboard standard button will affect input', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  for (let i = 65; i <= 90; i++) {
    const char = String.fromCharCode(i);
    const output = keyboard.utilities.getUpdatedInput(char, "test");
    expect(output).toBe(`test${char}`);
  }
});

it('Keyboard updateCaretPos will work with minus', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    syncInstanceInputs: true
  });

  keyboard.caretPosition = 5;
  keyboard.utilities.updateCaretPos(2, true);

  expect(keyboard.caretPosition).toBe(3);
});

it('Keyboard updateCaretPos will work with minus', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  keyboard.caretPosition = 5;
  keyboard.utilities.updateCaretPos(2, true);

  expect(keyboard.caretPosition).toBe(3);
});

it('Keyboard updateCaretPos will work with plus', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  keyboard.caretPosition = 5;
  keyboard.utilities.updateCaretPos(2);

  expect(keyboard.caretPosition).toBe(7);
});

it('Keyboard addStringAt will work with debug', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    debug: true
  });

  keyboard.getButtonElement("q").onclick();

  expect(keyboard.getInput()).toBe("q");
});

it('Keyboard addStringAt will work with position', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    debug: true
  });

  keyboard.setInput("test");
  keyboard.caretPosition = 4;

  keyboard.getButtonElement("q").onclick();

  expect(keyboard.getInput()).toBe("testq");
});

it('Keyboard addStringAt will respect maxLength', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    debug: true,
    maxLength: 4
  });

  keyboard.setInput("test");
  keyboard.caretPosition = 4;

  keyboard.utilities.handleMaxLength(keyboard.input, "testq")
  keyboard.utilities.addStringAt("test", "q", 4);

  expect(keyboard.caretPosition).toBe(4);
});

it('Keyboard handleMaxLength will exit out on same updatedInput', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    debug: true
  });

  keyboard.setInput("test");

  const output = keyboard.utilities.handleMaxLength(keyboard.input, "test")

  expect(output).toBeFalsy();
});

it('Keyboard handleMaxLength will work with object maxLength', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    maxLength: {
      default: 4
    }
  });

  keyboard.setInput("test");
  
  const output = keyboard.utilities.handleMaxLength(keyboard.input, "testq");

  expect(output).toBeTruthy();
});

it('Keyboard handleMaxLength will work with object maxLength and debug', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    maxLength: {
      default: 4
    },
    debug: true
  });

  keyboard.setInput("test");
  
  const output = keyboard.utilities.handleMaxLength(keyboard.input, "testq");

  expect(output).toBeTruthy();
});

it('Keyboard handleMaxLength will return false if obj maxLength not reached', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    maxLength: {
      default: 7
    }
  });

  keyboard.setInput("test");
  
  const output = keyboard.utilities.handleMaxLength(keyboard.input, "testq");

  expect(output).toBeFalsy();
});


it('Keyboard handleMaxLength will work without debug', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    maxLength: 4
  });

  keyboard.setInput("test");
  
  const output = keyboard.utilities.handleMaxLength(keyboard.input, "testq");

  expect(output).toBeTruthy();
});


it('Keyboard handleMaxLength will work with numeric maxLength', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    maxLength: 3
  });

  keyboard.setInput("test");
  
  const output = keyboard.utilities.handleMaxLength(keyboard.input, "testq");

  expect(output).toBe(true);
});

it('Keyboard handleMaxLength wont work with non numeric or object maxLength', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    maxLength: "wrong"
  });

  keyboard.setInput("test");
  
  const output = keyboard.utilities.handleMaxLength(keyboard.input, "testq");

  expect(output).toBeFalsy();
});

it('Keyboard handleMaxLength wont work with non numeric or object maxLength (with debug)', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    maxLength: "wrong",
    debug: true
  });

  keyboard.setInput("test");
  
  const output = keyboard.utilities.handleMaxLength(keyboard.input, "testq");

  expect(output).toBeFalsy();
});

it('Keyboard isMaxLengthReached will work', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    maxLength: 5
  });

  const output = keyboard.utilities.isMaxLengthReached();

  expect(output).toBeFalsy();
});

it('Keyboard removeAt will exit out on caretPosition:0', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  keyboard.setInput("test");
  keyboard.caretPosition = 0;
  keyboard.utilities.removeAt(keyboard.getInput(), 0);
  expect(keyboard.getInput()).toBe("test");

  keyboard.setInput("test");
  keyboard.caretPosition = 5;
  keyboard.utilities.removeAt(keyboard.getInput(), 0, true);
  expect(keyboard.caretPosition).toBe(4);
});

it('Keyboard removeAt will remove multi-byte unicodes with caretPos>0', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();

  keyboard.caretPosition = 6;
  let output = keyboard.utilities.removeAt("test\uD83D\uDE00", 6);
  expect(output).toBe("test");

  keyboard.caretPosition = 6;
  output = keyboard.utilities.removeAt("test\uD83D\uDE00", 6, true);
  expect(keyboard.caretPosition).toBe(4);
});

it('Keyboard removeAt will not remove multi-byte unicodes with caretPos:0', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard();
  let output = keyboard.utilities.removeAt("\uD83D\uDE00");
  expect(output).toBeFalsy();

  output = keyboard.utilities.removeAt("\uD83D\uDE00", 0,  true);
  expect(output).toBeFalsy();
});

it('Keyboard removeAt will remove regular strings', () => {
  testUtil.setDOM();

  const keyboard = new Keyboard({
    debug: true
  });

  keyboard.caretPosition = 6;
  let output = keyboard.utilities.removeAt("testie", 6);
  expect(output).toBe("testi");

  keyboard.caretPosition = 6;
  output = keyboard.utilities.removeAt("testie", 6, true);
  expect(keyboard.caretPosition).toBe(5);
});

it('Keyboard will work with custom (and weird) class', () => {
  testUtil.setDOM("my--weird--class");
  const keyboard = new Keyboard(".my--weird--class");
  expect(keyboard.keyboardDOMClass).toBe("my--weird--class");
});

it('Keyboard camelCase will work with empty strings', () => {
  testUtil.setDOM();
  const keyboard = new Keyboard();
  expect(keyboard.utilities.camelCase()).toBeFalsy();
});
