const fs = require('fs');
const path = require('path');

const viewer = fs.readFileSync(path.join(__dirname, 'web', 'viewer.html'), 'utf8');
const control = fs.readFileSync(path.join(__dirname, 'yjm_remote_control.py'), 'utf8');
const capture = fs.readFileSync(path.join(__dirname, 'yjm_win2rtc_capture.py'), 'utf8');

function ok(name, condition) {
  if (!condition) {
    console.error('FAIL', name);
    process.exitCode = 1;
  } else {
    console.log('OK', name);
  }
}

ok('double tap drag thresholds are bounded', viewer.includes('STAGE_DOUBLE_TAP_MS=360') && viewer.includes('STAGE_DOUBLE_TAP_PX=32') && viewer.includes('STAGE_DRAG_START_PX=8'));
ok('second touch must match time and position', viewer.includes('now-stageLastTap.at<=STAGE_DOUBLE_TAP_MS') && viewer.includes('touchDistance(pt,stageLastTap)<=STAGE_DOUBLE_TAP_PX'));
ok('drag starts only after movement threshold', viewer.includes('!stageDrag.active&&moved>=STAGE_DRAG_START_PX'));
ok('mouse down precedes drag move in handler', viewer.indexOf("action:'mouse_down'") >= 0 && viewer.indexOf("action:'drag_move'") > viewer.indexOf("action:'mouse_down'"));
ok('touch release sends mouse up and release all', viewer.includes("action:'mouse_up',button:0") && viewer.includes("action:'release_all'"));
ok('double touch without drag remains pointer move only', viewer.includes("send(Object.assign({type:'control',action:'move'},p))") && viewer.includes('원격화면 두 번 터치 포인터 이동 완료') && !viewer.includes('원격화면 더블터치 더블클릭'));
ok('two fingers cancel mouse drag before panning', viewer.includes("releaseRemoteDrag('two_finger_pan')") && viewer.includes('beginStagePan(ev)'));
ok('stuck button cleanup covers touch and lifecycle', viewer.includes("releaseRemoteDrag('touchcancel')") && viewer.includes("releaseRemoteDrag('visibility_hidden')") && viewer.includes("releaseRemoteDrag('pagehide')") && viewer.includes("releaseRemoteDrag('window_blur')") && viewer.includes("releaseRemoteDrag('disconnect')") && viewer.includes("releaseRemoteDrag('control_off')"));
ok('host supports down drag up release', control.includes('if action in ("move", "drag_move")') && control.includes('if action == "mouse_down"') && control.includes('if action == "mouse_up"') && control.includes('if action == "release_all"'));
ok('host release_all physically releases held mouse', control.includes('if self._mouse_is_down:') && control.includes('win32api.mouse_event(up, 0, 0, 0, 0)'));
ok('unexpected viewer disconnect releases input', capture.includes('viewer disconnect release_all failed') && capture.includes('self.control_handler(json.dumps({"type": "control", "action": "release_all"}))'));
ok('touchpad two finger wheel remains intact', viewer.includes("controlPayload('wheel',{delta:-dy*7})"));

if (!process.exitCode) console.log('OK test_rtc8790_mobile_double_tap_drag_v205');
