//story_component_checker_load
function story_component_checker_load() {
  //checker objeleri

  for (key in dataStory) {
    var dataObject = dataStory[key];

    if (dataObject["objectType"] == "checker") {
      //object
      var object = new Konva.Group({
        id: key,
        x: parseInt(dataObject["x"]),
        y: parseInt(dataObject["y"]),
        checked: null,
        data: dataObject,
      });
      grpStory.add(object);

      initButton(object, 0);
      object.on("pointerdown ", function () {
        story_component_checker_click(this);
      });

      //rect
      var rect = new Konva.Rect({
        x: 0,
        y: 0,
        width: parseInt(dataObject["width"]),
        height: parseInt(dataObject["height"]),
      });
      object.add(rect);

      //image
      var image;
      var imageSrc = new Image();
      imageSrc.onload = function () {};
      image = new Konva.Image({
        id: "image",
        x: parseInt(dataObject["imageX"]),
        y: parseInt(dataObject["imageY"]),
        width: parseInt(dataObject["imageWidth"]),
        height: parseInt(dataObject["imageHeight"]),
        image: imageSrc,
      });
      imageSrc.src = stagePath + dataObject["image"];
      object.add(image);

      //imageReset
      var imageReset;
      var imageResetSrc = new Image();
      imageResetSrc.onload = function () {};
      imageReset = new Konva.Image({
        id: "imageReset",
        x: parseInt(dataObject["imageX"]),
        y: parseInt(dataObject["imageY"]),
        width: parseInt(dataObject["imageWidth"]),
        height: parseInt(dataObject["imageHeight"]),
        image: imageResetSrc,
        visible: false,
      });
      imageResetSrc.src = stagePath + dataObject["imageReset"];
      object.add(imageReset);
    }
  }
}
function story_component_checker_click(object) {
  let image = object.findOne("#image");
  let imageReset = object.findOne("#imageReset");

  if (object.getAttr("checked")) {
    //check edilmiş, resetle
    image.show();
    imageReset.hide();
    object.setAttr("checked", null);
    story_component_checker_reset(object);
  } else {
    //check edilmemiş, check et
    image.hide();
    imageReset.show();
    object.setAttr("checked", "checked");
    story_component_checker_check(object);
  }
}

function story_component_checker_check(object) {
  var dataObject = object.getAttr("data");

  var result = null;
  var checker_total = 0;
  var checker_correct = 0;
  var videoSuccess = dataObject["videoSuccess"];
  var videoFail = dataObject["videoFail"];

  if (Array.isArray(dataObject["target"])) {
    //coklu obje
    checker_total = dataObject["target"].length;
    for (key in dataObject["target"]) {
      objectID = dataObject["target"][key];
      result = story_component_checker_check_component(objectID);
      if (result) {
        checker_correct++;
      }
    }
  } else {
    //tek obje
    checker_total = 1;
    objectID = dataObject["target"];
    result = story_component_checker_check_component(objectID);
    if (result) {
      checker_correct++;
    }
  }

  //play video eger video tanimlanmis ise
  if (checker_correct >= checker_total) {
    //success
    if (videoSuccess) {
      story_component_checker_play(stagePath + videoSuccess);
    }
  } else {
    //fail
    if (videoFail) {
      story_component_checker_play(stagePath + videoFail);
    }
  }
}

function story_component_checker_reset(object) {
  dataObject = object.getAttr("data");

  if (Array.isArray(dataObject["target"])) {
    //coklu obje
    for (key in dataObject["target"]) {
      objectID = dataObject["target"][key];
      story_component_checker_reset_component(objectID);
    }
  } else {
    //tek obje
    objectID = dataObject["target"];
    story_component_checker_reset_component(objectID);
  }
}

function story_component_checker_play(videoFile) {
  var video = document.getElementById("videoChecker");
  video.src = videoFile;
  video.play();
  video.style.display = "block";

  video.addEventListener("ended", function () {
    video.style.display = "none";
  });
}

function story_component_checker_check_component(objectID) {
  let obj = grpStory.findOne("#" + objectID);
  let data = obj.getAttr("data");
  let result = null;
  switch (data["objectType"]) {
    case "wordbox":
      result = story_component_wordbox_check_one(obj);
      break;
    case "colorbox":
      result = story_component_colorbox_check_one(obj);
      break;
    case "dragSource":
      result = story_component_drag_check_one(obj);
      break;
    case "grid":
      result = story_component_grid_check_one(obj);
      break;
    case "input":
      result = story_component_input_check_one(obj);
      break;
    case "match":
      result = matchCheck_one(obj);
      break;
    case "option":
      result = optionCheck_one(obj);
      break;
    case "button":
      result = story_component_button_check_one(obj);
      break;
  }
  return result;
}

function story_component_checker_reset_component(objectID) {
  let obj = grpStory.findOne("#" + objectID);
  let data = obj.getAttr("data");
  switch (data["objectType"]) {
    case "wordbox":
      story_component_wordbox_reset_one(obj);
      break;
    case "colorbox":
      result = story_component_colorbox_reset_one(obj);
      break;
    case "dragSource":
      result = story_component_drag_reset_one(obj);
      break;
    case "grid":
      result = story_component_grid_reset_one(obj);
      break;
    case "input":
      result = story_component_input_reset_one(obj);
      break;
    case "match":
      result = story_component_match_reset_one(obj);
      break;
    case "option":
      result = story_component_option_reset_one(obj);
      break;
    case "button":
      result = story_component_button_reset_one(obj);
      break;
  }
}
