//optionLoad
function optionLoad() {
  //option objeleri

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

    if (dataObject["objectType"] == "option") {
      //grpOption
      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 () {
        optionClick(this);
      });

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

      //check
      var check = qcImage({
        id: "check",
        x: parseInt(dataObject["imageX"]),
        y: parseInt(dataObject["imageY"]),
        width: parseInt(dataObject["imageWidth"]),
        height: parseInt(dataObject["imageHeight"]),
        src: dataObject["image"],
      });
      check.hide();
      object.add(check);
    }
  }
}
function optionClick(e) {
  story_check_unlock = true;

  group = e.getAttr("data")["group"];
  correct = e.getAttr("data")["correct"];
  check = e.findOne("#check");

  //tüm optionlari tara, tiklanan option ile ayni group olan optionlari resetle
  objects = grpStory.find("Group");

  for (key in objects) {
    var obj = objects[key];

    if (obj.getAttr("data") && obj.getAttr("data")["objectType"] == "option") {
      objGroup = obj.getAttr("data")["group"];
      objCheck = obj.findOne("#check");
      if (objGroup == group) {
        if (e != obj) {
          obj.setAttr("checked", false);
          objCheck.hide();
        }
      }
    }
  }

  if (e.getAttr("checked") == true) {
    e.setAttr("checked", false);
    check.hide();
  } else {
    e.setAttr("checked", true);
    check.show();
  }
}

function optionChecker(obj, trueFalse) {
  var imageSrc = new Image();
  imageSrc.onload = function () {
    var checker = new Konva.Image({
      id: "checker",
      x: parseInt(obj.getAttr("data")["checkerX"]),
      y: parseInt(obj.getAttr("data")["checkerY"]),
      width: parseInt(obj.getAttr("data")["checkerWidth"]),
      height: parseInt(obj.getAttr("data")["checkerHeight"]),
      image: imageSrc,
    });
    obj.add(checker);
  };

  if (trueFalse) {
    imageSrc.src = stagePath + obj.getAttr("data")["checkerCorrect"];
  } else {
    imageSrc.src = stagePath + obj.getAttr("data")["checkerWrong"];
  }
}

function optionCheck() {
  var objects = grpStory.getChildren();
  for (key in objects) {
    var obj = objects[key];

    if (obj.getAttr("data") && obj.getAttr("data")["objectType"] == "option") {
      obj.listening(false);

      storyTotalObject++;
      if (obj.getAttr("checked") == null) {
        //option hic secilmemis
        if (obj.getAttr("data")["correct"] == true) {
          if (obj.getAttr("data")["checkEmpty"]) {
            optionChecker(obj, false);
          }
        } else {
          storyTotalCorrect++;
          obj.setAttr("success", true);
          if (obj.getAttr("data")["checkEmpty"]) {
            optionChecker(obj, true);
          }
        }
      } else {
        //option secilmis
        if (obj.getAttr("data")["correct"] == obj.getAttr("checked")) {
          //dogru hesabimiza bu optioni dahil ediyoruz
          //cunku secilsin veya secilmesin dogru yapilmis
          storyTotalCorrect++;
          obj.setAttr("success", true);
          if (obj.getAttr("checked")) {
            optionChecker(obj, true);
          }
        } else {
          if (obj.getAttr("checked")) {
            optionChecker(obj, false);
          }
        }
      }
    }
  }
}

function optionSolve() {
  var objects = grpStory.getChildren();
  for (key in objects) {
    var obj = objects[key];
    if (obj.getAttr("data") && obj.getAttr("data")["objectType"] == "option") {
      checker = obj.findOne("#checker");
      if (checker) {
        checker.destroy();
      }

      obj.listening(false);

      check = obj.findOne("#check");

      if (obj.getAttr("data")["correct"] == "1") {
        check.show();
      } else {
        check.hide();
      }
    }
  }
}

function optionReset() {
  var objects = grpStory.getChildren();
  for (key in objects) {
    var obj = objects[key];
    if (obj.getAttr("data") && obj.getAttr("data")["objectType"] == "option") {
      obj.setAttr("checked", null);
      obj.setAttr("success", null);

      obj.listening(true);

      checker = obj.findOne("#checker");
      if (checker) {
        checker.destroy();
      }

      check = obj.findOne("#check");
      check.hide();
    }
  }
}
