#!/system/bin/sh
# Abre DMSS -> wizard inicial (si aplica) -> grupo NVR -> mosaico 4x4.
# Panel 1920x1080 density 320 (HS86Pro). Sin NVR: solo valida arranque/UI.

tap_center() {
  b="$1"
  rest=${b#*[}
  x1=${rest%%,*}
  rest2=${rest#*,}
  y1=${rest2%%]*}
  rest3=${b#*][}
  x2=${rest3%%,*}
  y2=${rest3#*,}
  y2=${y2%%]*}
  input tap $(( (x1 + x2) / 2 )) $(( (y1 + y2) / 2 ))
}

find_bounds() {
  rid="$1"
  uiautomator dump /data/local/tmp/kiosk-ui.xml >/dev/null 2>&1
  grep -o "resource-id=\"${rid}\"[^>]*bounds=\"\[[0-9,]*\]\[[0-9,]*\]\"" /data/local/tmp/kiosk-ui.xml 2>/dev/null \
    | head -1 | sed 's/.*bounds="\([^"]*\)".*/\1/'
}

count_cells() {
  uiautomator dump /data/local/tmp/kiosk-ui.xml >/dev/null 2>&1
  grep -o 'cl_content' /data/local/tmp/kiosk-ui.xml 2>/dev/null | wc -l | tr -d ' '
}

dismiss_dialogs() {
  uiautomator dump /data/local/tmp/kiosk-ui.xml >/dev/null 2>&1
  if grep -q 'text="Aceptar"' /data/local/tmp/kiosk-ui.xml 2>/dev/null; then
    input tap 778 395
    sleep 2
  fi
  if grep -q 'rooteado' /data/local/tmp/kiosk-ui.xml 2>/dev/null; then
    input tap 1252 22
    sleep 1
  fi
}

setup_first_run() {
  uiautomator dump /data/local/tmp/kiosk-ui.xml >/dev/null 2>&1
  if grep -q 'privacy_check' /data/local/tmp/kiosk-ui.xml 2>/dev/null; then
    b=$(find_bounds "com.mm.android.DMSS:id/privacy_check")
    if [ -n "$b" ]; then tap_center "$b"; else input tap 722 796; fi
    sleep 1
    b=$(find_bounds "com.mm.android.DMSS:id/privacy_next")
    if [ -n "$b" ]; then tap_center "$b"; else input tap 960 920; fi
    sleep 3
  fi
  i=0
  while [ "$i" -lt 6 ]; do
    uiautomator dump /data/local/tmp/kiosk-ui.xml >/dev/null 2>&1
    if grep -q 'iv_next' /data/local/tmp/kiosk-ui.xml 2>/dev/null; then
      b=$(find_bounds "com.mm.android.DMSS:id/iv_next")
      if [ -n "$b" ]; then tap_center "$b"; else input tap 960 924; fi
      sleep 2
      i=$((i + 1))
      continue
    fi
    if grep -qE 'SplashCountrySelectActivity|seleccionar el país|País o zona' /data/local/tmp/kiosk-ui.xml 2>/dev/null; then
      if grep -q 'common_alertdialog_confirm' /data/local/tmp/kiosk-ui.xml 2>/dev/null; then
        b=$(find_bounds "com.mm.android.DMSS:id/common_alertdialog_confirm")
        if [ -n "$b" ]; then tap_center "$b"; else input tap 960 701; fi
        sleep 2
      fi
      input tap 1180 80
      sleep 2
      input tap 960 280
      sleep 3
      break
    fi
    break
  done
}

apply_split_16() {
  input tap 960 540
  sleep 2
  b=$(find_bounds "com.mm.android.DMSS:id/split_btn")
  if [ -n "$b" ]; then
    tap_center "$b"
    sleep 1
    tap_center "$b"
  else
    input tap 580 1047
    sleep 1
    input tap 580 1047
  fi
  sleep 2
  input tap 1498 378
  sleep 3
}

pm enable com.mm.android.DMSS 2>/dev/null
sh /data/local/tmp/dmss-config.sh
am force-stop com.mm.android.DMSS 2>/dev/null
sleep 2
monkey -p com.mm.android.DMSS -c android.intent.category.LAUNCHER 1 >/dev/null 2>&1
sleep 12
setup_first_run
dismiss_dialogs

b=$(find_bounds "com.mm.android.DMSS:id/aciv_play_all")
if [ -n "$b" ]; then tap_center "$b"; else input tap 1752 220; fi
sleep 12
dismiss_dialogs

CELLS=$(count_cells)
CELLS=${CELLS:-0}
if [ "$CELLS" -lt 16 ] 2>/dev/null; then
  apply_split_16
  CELLS=$(count_cells)
  CELLS=${CELLS:-0}
fi

log -t cctv_kiosk "DMSS mosaico 16 (celdas=$CELLS)"
