// =====================================================================
// CENA E-notas (8,05–10,80 s, ref 9,50): notas do Instagram em modo escuro + linha de conversa.
// Mundo = quadro 9,50 s do original. Duas partes:
//   - UI FIXA (fundo, trilho de ícones, "Pesquisar", linha "Render Farm") — PNG ui.png, filha do CAM
//   - TRILHO (fileira de notas) — null filho do CAM que ROLA para a esquerda (no original a fileira rola de nota em nota
//     enquanto a busca e o trilho de ícones ficam parados; medido pelo avatar de mari e pelo ícone amarelo)
//     filhos: notas.png, 2 balões digitados (slots E2/E3 do letra.json, fade letra a letra como no original) e a CENA D-spotify na bolinha "Your note"
// A fileira é recortada em x >= 100 (borda da área de conteúdo, como no original) a partir de 8,46 s.
// Câmera: camera/cena-E-notas.json com 8,04–8,75 e 10,1–10,8 corrigidos (a medição seguia a fileira, que rola).
// Rodar: P = "<raiz>/"; $.evalFile(P + "ae/E-notas.jsx");   (depois de D-spotify.jsx)
// =====================================================================
$.evalFile(P + "ae/w-midia-comum.jsx");
$.evalFile(P + "ae/camera.jsx");
$.evalFile(P + "ae/letra.jsx");

var E = {};
E.pasta = WM.pasta("w-midia");
E.pastaAssets = WM.pasta("w-midia assets");
E.ESPACO = 412;                                 // distância entre avatares
E.X0 = -599;                                    // x da bolinha "Sua nota" com a fileira na posição do quadro 9,50
E.YAV = 568.36;                                 // y do centro da bolinha "Sua nota" (= mira y do CAM em 8,04)
// deslocamento da fileira (R) por tempo, em px do mundo — ajustado medindo os balões (fileira) e o círculo amarelo (UI) contra o original, quadro a quadro
E.TRILHO = [[8.0417, 820], [8.375, 815], [8.4167, 805], [8.4583, 777], [8.5, 738], [8.5417, 699], [8.5833, 669], [8.625, 597.5], [8.6667, 528.6],
  [8.7083, 475.8], [8.75, 442], [8.7917, 409], [8.8333, 390.5], [8.875, 373.8], [8.9167, 357.4], [9.0, 324.5], [9.125, 278], [9.25, 197], [9.375, 92], [9.5, 0],
  [9.625, -49], [9.75, -75], [9.875, -88], [10.0, -92], [10.0833, -95]];
// mira X corrigida (no começo a medição andava com a fileira; no fim, com o zoom em mari)
E.MX_INI = [[8.0417, 221.2], [8.0833, 231.8], [8.125, 243.0], [8.1667, 254.4], [8.2083, 267.1], [8.25, 281.4], [8.2917, 298.0],
  [8.3333, 317.6], [8.375, 336.1], [8.4167, 354.7], [8.4583, 362.5], [8.5, 370.2], [8.5417, 392], [8.5833, 415.6], [8.625, 447.7], [8.6667, 472.3],
  [8.7083, 487.3], [8.75, 499.5], [8.7917, 508.2], [8.8333, 516.2], [8.875, 520.7], [8.9167, 526.0], [9.0, 532.8]];
E.MX_FIM = [[10.0417, 540], [10.0833, 538.5], [10.1667, 538], [10.25, 537.5], [10.3333, 539], [10.5, 539.5], [10.75, 545.5], [10.7917, 547.5]];
E.MY_FIM = [[10.0417, 540], [10.1667, 540], [10.3333, 540.5], [10.5, 540.5], [10.75, 541], [10.7917, 541]];   // conferido pela caixa do balão de mari em 10,75 (topo y 67 como no original)
// balões digitados: texto do letra.json (slots E2 e E3, caixa alta), caixa de parágrafo que quebra em 2 linhas como no original.
// [nome, slot, índice do avatar, largura da caixa, janelas [início, fim] de cada palavra MEDIDAS no original (fade letra a letra)]
E.BALOES = [
  ["balão the.producer (slot E2)", "E2", 2, 290, [[8.66, 8.85], [8.90, 8.95], [8.96, 9.20]]],
  ["balão mari.frames (slot E3)", "E3", 3, 270, [[9.20, 10.08], [10.15, 10.78]]]
];
E.TOPO_TEXTO = 344;                              // topo da caixa de texto (conferido com a letra colada: linhas no mesmo lugar do original)
// chaves P(t) = letras já digitadas (contando espaços); palavras a mais seguem a 0,06 s por letra
E.chavesDigita = function (palavras, janelas) {
  var keys = [], c = 0, k, j;
  for (k = 0; k < palavras.length; k++) {
    j = janelas[k];
    if (!j) { var t0 = keys[keys.length - 1][0] + 0.04; j = [t0, t0 + 0.06 * palavras[k].length]; }
    keys.push([j[0], c]); c += palavras[k].length; keys.push([j[1], c]); c += 1;
  }
  keys.push([keys[keys.length - 1][0] + 0.17, c + 2.5]);   // termina o fade da última letra
  return keys;
};

var cD = WM.item("CENA D-spotify");
if (!cD) throw new Error("rode D-spotify.jsx antes (falta CENA D-spotify)");
var ftUI = WM.png(P + "assets/E-notas/ui.png", E.pastaAssets);
var ftNo = WM.png(P + "assets/E-notas/notas.png", E.pastaAssets);
var cena = WM.comp("CENA E-notas", 1080, 1080, E.pasta);
var cam = CAMERA_criar(cena, P + "camera/cena-E-notas.json");
var fx = cam.property("ADBE Effect Parade"), k;
WM.trocarChaves(fx.property("Mira X").property(1), 8.03, 9.01, E.MX_INI);
WM.trocarChaves(fx.property("Mira X").property(1), 10.03, 10.8, E.MX_FIM);
WM.trocarChaves(fx.property("Mira Y").property(1), 10.03, 10.8, E.MY_FIM);
var CAM = cena.layer("CAM");

function zerar(L, ax, ay, px, py, esc) {
  var tr = L.property("ADBE Transform Group");
  tr.property("ADBE Anchor Point").setValue([ax, ay]); tr.property("ADBE Position").setValue([px, py]);
  tr.property("ADBE Scale").setValue([esc, esc]); tr.property("ADBE Rotate Z").setValue(0);
}
// 1. UI fixa
var ui = cena.layers.add(ftUI); ui.name = "UI fixa (fundo, ícones, busca, conversa)"; ui.parent = CAM;
zerar(ui, 0, 0, -400, -100, 100 / 3);
// sempre visível: até 8,6 os ícones e a busca estão fora da tela; o fundo #090F11 dele é o fundo do Instagram atrás da bolinha D
// 2. recorte da fileira (x >= 100 no mundo) — liga em 8,4583, quando a borda já está fora da tela
var rec = cena.layers.addShape(); rec.name = "RECORTE fileira (x>=100)"; rec.parent = CAM;
var rg = rec.property("ADBE Root Vectors Group").addProperty("ADBE Vector Group"); rg.name = "ret";
var rv = rec.property("ADBE Root Vectors Group").property("ret").property("ADBE Vectors Group");
rv.addProperty("ADBE Vector Shape - Rect"); rv.addProperty("ADBE Vector Graphic - Fill");
rv = rec.property("ADBE Root Vectors Group").property("ret").property("ADBE Vectors Group");
rv.property(1).property("ADBE Vector Rect Size").setValue([6000, 3000]);
WM.chaves(rv.property(1).property("ADBE Vector Rect Position"), [[0, [-500, 540]], [8.4583, [100 + 3000, 540]]], "hold");
zerar(rec, 0, 0, 0, 0, 100);
// 3. TRILHO (fileira que rola)
var tri = cena.layers.addNull(WM.DUR); tri.name = "TRILHO (fileira de notas rola)"; tri.enabled = false; tri.label = 9; tri.parent = CAM;
zerar(tri, 0, 0, 0, 0, 100);
var ptri = [];
for (k = 0; k < E.TRILHO.length; k++) ptri.push([E.TRILHO[k][0], [E.TRILHO[k][1], 0]]);
WM.chaves(tri.property("ADBE Transform Group").property("ADBE Position"), ptri, "suave");
// 3a. fileira (PNG)
var no = cena.layers.add(ftNo); no.name = "fileira de notas (PNG 3x)"; no.parent = tri;
zerar(no, 0, 0, -900, 250, 100 / 3);
// 3b. balões digitados
var bal = [];
for (k = 0; k < E.BALOES.length; k++) {
  var B = E.BALOES[k], slot = LETRA(B[1]), txt = slot.texto.toUpperCase(), Wc = B[3];
  var L = cena.layers.addBoxText([Wc, 130]); L.name = B[0];
  var pd = L.property("ADBE Text Properties").property("ADBE Text Document"), d = pd.value;
  d.resetCharStyle(); d.resetParagraphStyle();
  d.font = "SegoeUI-Bold"; d.fontSize = 42; d.fillColor = WM.cor("#F6F7FB"); d.applyFill = true; d.applyStroke = false;
  d.tracking = 12; d.autoLeading = false; d.leading = 52; d.justification = ParagraphJustification.CENTER_JUSTIFY; d.text = txt;
  pd.setValue(d);
  d = pd.value; try { d.boxTextPos = [-Wc / 2, 0]; pd.setValue(d); } catch (eB) {}
  L.startTime = 0; L.inPoint = 0; L.outPoint = WM.DUR;
  L.parent = tri; zerar(L, 0, 0, E.X0 + E.ESPACO * B[2], E.TOPO_TEXTO, 100);
  var tp = L.property("ADBE Text Properties");
  tp.property("ADBE Text Animators").addProperty("ADBE Text Animator").name = "digita";
  var an = tp.property("ADBE Text Animators").property("digita");
  an.property("ADBE Text Animator Properties").addProperty("ADBE Text Opacity");
  an = tp.property("ADBE Text Animators").property("digita");
  an.property("ADBE Text Animator Properties").property("ADBE Text Opacity").setValue(0);
  an.property("ADBE Text Selectors").addProperty("ADBE Text Selector");
  var sel = tp.property("ADBE Text Animators").property("digita").property("ADBE Text Selectors").property(1);
  var adv = sel.property("ADBE Text Range Advanced");
  adv.property("ADBE Text Range Units").setValue(2); adv.property("ADBE Text Range Type2").setValue(1); adv.property("ADBE Text Range Shape").setValue(2);
  var pk = E.chavesDigita(txt.replace(/^\s+|\s+$/g, "").split(/\s+/), B[4]), fimL = [], iniL = [];
  for (var j = 0; j < pk.length; j++) { fimL.push([pk[j][0], pk[j][1]]); iniL.push([pk[j][0], pk[j][1] - 2.5]); }
  WM.chaves(sel.property("ADBE Text Index End"), fimL, "linear");
  WM.chaves(sel.property("ADBE Text Index Start"), iniL, "linear");
  bal.push(L);
}
// 3c. CENA D na bolinha "Sua nota": enche a tela em 8,0417; máscara quadrado -> círculo até 8,4167 (no original 8,04–8,42)
var Ld = cena.layers.add(cD); Ld.name = "ENCAIXE D-spotify (já é a CENA D-spotify real)"; Ld.parent = tri;
var e0 = 100 * (1080 / Math.pow(2, 2.0449)) / 1080, e1 = 100 * 300 / 1080;
zerar(Ld, 540, 540, E.X0, E.YAV, e0);
Ld.property("ADBE Transform Group").property("ADBE Scale").setValuesAtTimes([8.0417, 8.4167], [[e0, e0], [e1, e1]]);
WM.easeTodas(Ld.property("ADBE Transform Group").property("ADBE Scale"), 30, 60);
var md = Ld.property("ADBE Mask Parade").addProperty("ADBE Mask Atom"); md = Ld.property("ADBE Mask Parade").property(1); md.name = "quadrado -> círculo";
var dCirc = 290 / (e1 / 100);
// a camada é colapsada: a janela inteira da D aparece além do quadrado (como no original, a barra de busca surge em cima em 8,17).
// máscara em px da CENA D: começa no contorno da janela (x -400..973 = corte da nota, y -87..1119) e vira o círculo até 8,4167
Ld.collapseTransformation = true;
function caixa(x0, x1, y0, y1, r) { return WM.retRed((x0 + x1) / 2, (y0 + y1) / 2, x1 - x0, y1 - y0, r); }
var c0 = 540 - dCirc / 2, c1 = 540 + dCirc / 2;
var mt = [8.0417, 8.1667, 8.25, 8.3333, 8.4167], ms = [];
ms.push(caixa(-400, 973, -87, 1119, 30)); ms.push(caixa(-330, 973, -87, 1119, 55)); ms.push(caixa(-200, 975, -75, 1110, 110));
ms.push(caixa(-20, 1030, -10, 1090, 330)); ms.push(caixa(c0, c1, c0, c1, dCirc / 2));
md.property("ADBE Mask Shape").setValuesAtTimes(mt, ms);
// recorte (matte) na fileira, nos balões e na bolinha D
var recL = cena.layer("RECORTE fileira (x>=100)");
no.setTrackMatte(recL, TrackMatteType.ALPHA);
for (k = 0; k < bal.length; k++) bal[k].setTrackMatte(recL, TrackMatteType.ALPHA);
Ld.setTrackMatte(recL, TrackMatteType.ALPHA);
recL.enabled = false;
// ordem (de cima p/ baixo): CAM, D, balões, fileira, TRILHO, recorte, UI
CAM.moveToBeginning();
WM.blurTodas(cena); CAM.motionBlur = false; tri.motionBlur = false;
WM.limparSolidos();
"ok E: " + cena.numLayers + " camadas";
