localforage.config({
    name: 'notestorage'
});
var KiBhaiNoteKoyta = '0';
localforage.length(function(e, v) {
  KiBhaiNoteKoyta = v;
});
document.querySelector(".ion-plus").addEventListener('click', function() {
  document.querySelector("#note-add").style.display = "block";
  document.querySelector("#noteList").style.display = "none";
});
var noteTitle = document.querySelector('.notetitle').value;
var noteBody = document.querySelector('.notebody').value;
var Gaan = '';
var Chobi = '';
var ChobiHTML = '';
var GaanHTML = '';
document.querySelector('.notetitle').addEventListener('input', function() {
  noteTitle = document.querySelector('.notetitle').value;
});
document.querySelector('.notebody').addEventListener('input', function() {
  noteBody = document.querySelector('.notebody').value;
});

document.querySelector(".back-button").addEventListener('click', function() {
  location.reload(true);
});
document.querySelector(".pick-image").addEventListener('click', function() {
  var pickImage = new MozActivity({
    name: "pick",
    data: {
      type: ["image/png", "image/jpg", "image/jpeg"] 
    }
  });

  pickImage.onsuccess = function() { 
    // the blob
    Chobi = this.result.blob;
    alert('Image has been added');
    // make into a data uri
    //Chobi = window.URL.createObjectURL(this.result.blob);
    // and use the data uri, obvs
  };

  pickImage.onerror = function() { };
});

document.querySelector(".pick-media").addEventListener('click', function() {
  var pickMedia = new MozActivity({
    name: "pick",
    data: {
      type: ["audio/mpe3", "audio/ogg", "video/mp4", "video/3gpp"] 
    }
  });

  pickMedia.onsuccess = function() { 
    // the blob
    Gaan = this.result.blob;
    alert('Media file has been added');
    // make into a data uri
    //console.log(window.URL.createObjectURL(this.result.blob));
    // and use the data uri, obvs
  };

  pickMedia.onerror = function() { };
});

document.querySelector(".save-button").addEventListener('click', function() {
  localforage.setItem(Math.random() + KiBhaiNoteKoyta + 1, {
    "title": noteTitle,
    "body": noteBody,
    "chobi": Chobi,
    "gaan": Gaan
  }, function(e, v) {
    console.log(v);
    alert('Note has been saved');
    location.reload(true);
  });
});
var noteList = "";
localforage.iterate(function(value, key) {
  console.log([key]);
  noteList += '<div ' + ' data-notedekhao="' + [key][0] + '" ' + ' class="note-view item item-button-right"><h2>' + [value][0]["title"] + '</h2><p>' + [value][0]["body"] + '</p><button class="button button-clear notes note' + [key][0] + '"' + 'data-id="' + [key][0] + '"' + '><i class="icon ion-ios7-trash"></i></button></div>';
  document.getElementById('noteList').innerHTML = '<div class="list">' + noteList + '</div>';
}, function(v, k) {
  var BhejalNoteKhata = document.getElementsByClassName('notes');
  console.log(BhejalNoteKhata);
  for (var i = 0; i < BhejalNoteKhata.length; i++) {
    BhejalNoteKhata[i].onclick = function() {
      localforage.removeItem(this.getAttribute('data-id'), function(err) {
        console.log('Key is cleared!');
        alert('Your note has been deleted');
        location.reload(true);
      });
    }
  }
  var noteDekhao = document.getElementsByClassName('note-view');
  console.log(noteDekhao);
  for (var i = 0; i < noteDekhao.length; i++) {
    noteDekhao[i].onclick = function() {
      console.log(this.getAttribute('data-notedekhao'));
      localforage.getItem(this.getAttribute('data-notedekhao'), function(err, value) {
        console.log(value);

        if (value['gaan'] == '') {
          GaanHTML = '';
        } else {
          GaanHTML = '<audio class="notegaan" src="' + window.URL.createObjectURL(value['gaan']) + '"></audio><div class="button-bar"><button class="button button-action button-positive play-button">Play</button><button class="button button-action button-positive pause-button">Pause</button><button class="button button-action button-positive stop-button">Stop</button></div>';
        }
        if (value['chobi'] == '') {
          ChobiHTML = '';
        } else {
          ChobiHTML = '<img class="notechobi" src="' + window.URL.createObjectURL(value['chobi']) + '"/>';
        }
        document.getElementById('noteList').innerHTML = '<div class="list card"><div class="item bar-positive"><h1 class="notedekhanortitle">' + value['title'] + '</h1></div><div class="item item-text-wrap notedekhanorbody">' + value['body'] + '</div>' + ChobiHTML + GaanHTML + '<div class="item item-divider notedekhanorfoot"><i class="ion-ios7-undo"></i> Go Back</div></div>';
        document.querySelector(".notedekhanorfoot").addEventListener('click', function() {
          location.reload(true);
        });
        document.querySelector(".play-button").addEventListener('click', function() {
          document.querySelector('.notegaan').play();
        });
        document.querySelector(".pause-button").addEventListener('click', function() {
          document.querySelector('.notegaan').pause();
        });
        document.querySelector(".stop-button").addEventListener('click', function() {
          document.querySelector('.notegaan').currentTime = '0';
          document.querySelector('.notegaan').pause();
        });
      });
    }
  }
  console.log('Iteration has completed');
});
