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

const viewer = fs.readFileSync(path.join(__dirname, 'web', 'viewer.html'), 'utf8');

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

function between(source, startText, endText) {
  const start = source.indexOf(startText);
  const end = source.indexOf(endText, start + startText.length);
  if (start < 0 || end < 0) return '';
  return source.slice(start, end);
}

const stageTouchStart = between(
  viewer,
  "stageEl.addEventListener('touchstart'",
  "stageEl.addEventListener('touchmove'"
);
const stageTouchMove = between(
  viewer,
  "stageEl.addEventListener('touchmove'",
  "stageEl.addEventListener('touchend'"
);
const stageTouchEnd = between(
  viewer,
  "stageEl.addEventListener('touchend'",
  "stageEl.addEventListener('touchcancel'"
);

ok('v206 viewer build is v313', viewer.includes("const BUILD_TAG='v313'") && viewer.includes('RTC8790_8791 viewer v313'));
ok('pointer find UI is removed', !viewer.includes('id="pointerFindBtn"') && !viewer.includes('id="mobilePointerFindBtn"') && !viewer.includes('data-cmd="/cursor"'));
ok('first tap is stored for second-touch matching', stageTouchEnd.includes('stageLastTap={at:performance.now(),x:t.clientX,y:t.clientY}'));
ok('second touch requires bounded time and position', stageTouchStart.includes('now-stageLastTap.at<=STAGE_DOUBLE_TAP_MS') && stageTouchStart.includes('touchDistance(pt,stageLastTap)<=STAGE_DOUBLE_TAP_PX'));
ok('second touch immediately updates crosshair then moves Windows pointer', stageTouchStart.indexOf('updateRemotePointerFromPoint(p)') >= 0 && stageTouchStart.indexOf("action:'move'") > stageTouchStart.indexOf('updateRemotePointerFromPoint(p)'));
ok('second touch creates drag candidate without clicking', stageTouchStart.includes('stageDrag={id:t.identifier') && !stageTouchStart.includes("action:'click'") && !stageTouchStart.includes("action:'dblclick'"));
ok('held second touch starts left mouse down after threshold', stageTouchMove.includes('moved>=STAGE_DRAG_START_PX') && stageTouchMove.includes("action:'mouse_down',button:0"));
ok('held movement sends drag_move after mouse_down', stageTouchMove.indexOf("action:'drag_move'") > stageTouchMove.indexOf("action:'mouse_down'"));
ok('release after drag sends mouse_up and release_all', stageTouchEnd.includes("action:'mouse_up',button:0") && stageTouchEnd.includes("action:'release_all'"));
ok('release without movement does not click or double-click', stageTouchEnd.includes('원격화면 두 번 터치 포인터 이동 완료') && !stageTouchEnd.includes("action:'click'") && !stageTouchEnd.includes("action:'dblclick'"));
ok('two-finger stage pan is preserved', viewer.includes("releaseRemoteDrag('two_finger_pan')") && stageTouchMove.includes('setViewPan(stagePanStart.panX+(cx-stagePanStart.cx),stagePanStart.panY+(cy-stagePanStart.cy))'));
ok('touchpad wheel remains preserved', viewer.includes("controlPayload('wheel',{delta:-dy*7})"));

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