/* ============================================================
   assets.js - Preloads the PNG graphics used for characters and
   background. Each image is exposed by a logical key. Loading is
   fault-tolerant: a failed image simply resolves to null so the
   renderer can fall back to procedural (vector) drawing.
   ============================================================ */
(function (ST) {
  "use strict";

  var BASE = "about/images/png/";

  // logical key -> filename
  var FILES = {
    // characters (two frames each)
    snoopy_ready:  "snoopy_ready.png",
    snoopy_swing:  "snoopy_swing.png",
    charlie_ready: "charlie_ready.png",
    charlie_serve: "charlie_serve.png",
    lucy_idle:     "lucy_idle.png",
    lucy_smash:    "lucy_smash.png",
    snoopy_sleep:  "snoopy_sleep.png",
    // props / icons
    woodstock:     "woodstock.png",
    ball:          "ball.png",
    miss_lit:      "miss_lit.png",
    miss_unlit:    "miss_unlit.png",
    // background pieces
    bg_platform:   "bg_platform.png",
    bg_tree:       "bg_tree.png",
    bg_leaves:     "bg_leaves.png",
    bg_nest:       "nest.png",
    bg_doghouse:   "doghouse.png",
    bg_grass:      "bg_grass.png"
  };

  var images = {};
  var loaded = 0, total = 0, ready = false;

  function load(cb) {
    var keys = Object.keys(FILES);
    total = keys.length;
    if (total === 0) { ready = true; if (cb) cb(); return; }

    function done() {
      loaded++;
      if (loaded >= total) { ready = true; console.log("[assets] all graphics loaded"); if (cb) cb(); }
    }

    keys.forEach(function (k) {
      var img = new Image();
      img.onload = done;
      img.onerror = function () { console.warn("[assets] failed to load " + FILES[k]); images[k] = null; done(); };
      img.src = BASE + FILES[k];
      images[k] = img;
    });
  }

  // returns a usable, fully-decoded image or null
  function get(k) {
    var i = images[k];
    return (i && i.complete && i.naturalWidth > 0) ? i : null;
  }

  ST.Assets = {
    load: load,
    get: get,
    isReady: function () { return ready; },
    images: images
  };
})(window.ST = window.ST || {});
