// 名前空間
var ARCHIVE_UI = {};

$(function() {
  // アーカイブ検索
  $('#archive .search_form input[type="search"]').keyup(function(e) {


    var keyword = $('#archive .search_form input[type="search"]').val();
    if (e.KeyCode == 13 || e.key == "Enter") {
        if (keyword.length != 0 && keyword != '') {
            $('#archive .search_form input[type="search"]').blur();
            return;
        }
    } else if (keyword == '' || keyword.length < 2) {
        $('.overlay_src_msg').empty();
        return;
    }
    $('.overlay_src_msg').empty();
    if (CHAT_UTIL.isIOS()) {
        CHAT_DB.callGetArchiveByName(keyword);
    } else if (CHAT_UTIL.isAndroid()) {
        ARCHIVE_UI.refreshSearchScreen(keyword);
    }

    if (e.KeyCode == 13 || e.key == "Enter") {
       $('#archive .search_form input[type="search"]').blur();
       return;
    }
    // 検索結果を表示
  });
});

ARCHIVE_UI.refreshSearchScreen = function(keyword) {
  var archiveList = CHAT_DB.getArchiveByName(keyword);
  var archiveTemplate;
  $.get({ url: "./template/template_archive_list.html", async: false }
      , function(text) {
        archiveTemplate = text;
  });

  archiveList.forEach(function(archive) {
    var typeImage = "";
    switch(archive.archiveType) {
      case 0: // 画像
        typeImage = "icon/icon_collabo_picture.png";
        break;
      case 1: // 動画
        typeImage = "icon/icon_collabo_videocam.png";
        break;
      case 2: // 音声
        typeImage = "icon/icon_collabo_headset.png";
        break;
      case 3: // 文書
        typeImage = "icon/icon_collabo_document.png";
        break;
      default: // その他
        typeImage = "";
    }

    let html = Mustache.render(archiveTemplate, {
      archiveId: archive.archiveId,
      fileName: archive.archiveName,
      insertDate: archive.archiveDate,
      typeImage: typeImage
    });
      
    let obj = jQuery.parseHTML(html);
    $('.overlay_src_msg').append(obj);
  });
};
