// ===================================================================
// Class for showing list of second (related) Parol.
//

function SecondPageView(aModel, aController)
{
  try {
    PageView.call(this, "SecondList", "SecondCurIndex", "", "LastPair", "selectedParol");

    this._Model = aModel;
    this._Controller = aController;
    this._prefixForID = "secondLine";
    this._firstParolQual = null;
    this._PageSize = 50000; // show one page only (bible has ~ 30000 verses)
  }
  catch (e) {
    alert("SecondPageView: " + e);
  }
}

SecondPageView.prototype = new PageView();
SecondPageView.prototype.constructor = SecondPageView;

SecondPageView.prototype.toString = function()
{
  return "SecondPageView";
}

SecondPageView.prototype.getCurParolQual = function()
{
  var aPair = this.getCurCollectionItem();
  return aPair ? aPair.getSecond() : "";
}

SecondPageView.prototype.getCurParol = function()
{
  var parolQual = this.getCurParolQual();
  if (!parolQual) {
    return null;
  }
  var aParolBox = this._Model.getParolBox();
  return aParolBox ? aParolBox.findParol(parolQual) : null;
}

// set, no visual update
SecondPageView.prototype.setFirstParolQual = function(firstParolQual)
{
  this._firstParolQual = firstParolQual;
  this.updateForFirstParolQual();
}

// set, no visual update
SecondPageView.prototype.deleteSecond = function()
{
  if (this._Collection.length) {
    var index = this._CurIndex;
    this._Collection.splice(index, 1);
    this.updateCollection(); // sets index 0 :-(
    if (index >= this._Collection.length) {
      --index;
    }
    this.setCurIndex(index); // restore/decrement
  }
}

//// set, no visual update
//SecondPageView.prototype.deleteParol = function(parolQual)
//{
//  for (var i = 0, len = this._Collection.length; i < len; ++i) {
//    if (this._Collection[i].getID() == parolQual) {
//      // found parolQual, delete it
//      this._Collection.splice(i, 1);
//      var index = this._CurIndex;
//      this.updateCollection(); // sets index 0 :-(
//      if (this._Collection.length) {
//        if (index >= this._Collection.length) {
//          --index;
//        }
//        this.setCurIndex(index); // restore/decrement
//      }
//      return;
//    }
//  }
//}

// set, no visual update
SecondPageView.prototype.updateForFirstParolQual = function()
{
  var aSecondPairSet = this._Model.getPairSet().findSecondForFirst(this._firstParolQual);
  this.setCollection(aSecondPairSet);
  this.sortCollectionByRateBibleRef();
}

// sort, no visual update
SecondPageView.prototype.sortCollectionByBibleRef = function()
{
  var aSecondPairSet = this.getCollection();
  var parolQual = this.getCurParolQual();
  aSecondPairSet.sort(
      function(a, b) {
        //TODO low HS 2009-12-31 - use cache for sorting second list by bible reference
        return Bible20.Bible.BibleRef.compare(a._secondBibleRef, b._secondBibleRef);
      }
    );
  this.setCollection(aSecondPairSet);
  this.gotoParolQual(parolQual);
}

// sort, no visual update
SecondPageView.prototype.sortCollectionByRateBibleRef = function()
{
  var aSecondPairSet = this.getCollection();
  var parolQual = this.getCurParolQual();
  aSecondPairSet.sort(
    // descending
      function(a, b) {
        //TODO low HS 2009-12-31 - use cache for sorting second list by user rating and bible reference
        var rate = (b.getYes() - b.getNo()) - (a.getYes() - a.getNo());
        if (rate) {
          return rate;
        }
        //if (!a._secondBibleRef) { s += "\nNo secondBibleRef in a " + a; return 0; }
        //if (!b._secondBibleRef) { s += "\nNo secondBibleRef in b " + b; return 0; }
        // in case a._secondBibleRef is not set: call to PairSet.updateParolCache is missing in BibleStudioModel
        return Bible20.Bible.BibleRef.compare(a._secondBibleRef, b._secondBibleRef);
      }
    );
  this.setCollection(aSecondPairSet);
  this.gotoParolQual(parolQual);
}


// set index, no visual update (call updatePage() to redisplay)
SecondPageView.prototype.gotoParolQual = function(parolQual)
{
  var aSecondPairSet = this.getCollection();
  var index = null;
  if (parolQual) {
    for (var i = 0, len = aSecondPairSet.length; i < len; ++i) {
      var aPair = aSecondPairSet[i];
      if (aPair.getSecond() == parolQual) {
        index = i;
        break;
      }
    }
  }
  // alert("gotoParolQual " + parolQual + " => index= " + index);
  return this.setCurIndex(index);// handles null
}

SecondPageView.prototype.findParolIndex = function(aParol)
{
  var parolQual = aParol.getID();
  for (var i = 0, Coll = this.getCollection(), len = Coll.length; i < len; ++i) {
    var aPair = Coll[i];
    if (aPair.getSecond() == parolQual) {
      return i;
    }
  }
  return null;
}



// --- overrides ---

SecondPageView.prototype.notifyUpdatePage = function()
{
  this._Controller.selectSecondLine(this.getCurIndex() - this.getPageStartIndex());
}

SecondPageView.prototype.getElementForLineIndex = function(lineIndex)
{
  return document.getElementById(this._prefixForID + lineIndex);
}

SecondPageView.prototype._computeEmptyPage = function()
{
  return "<p>Dem gew\u00e4hlten Erstspruch wurde noch kein Zweitspruch zugeordnet.</p>";
}

SecondPageView.prototype._computePageHeader = function()
{
  return "<table class='parolTable'>"
    + "<colgroup>"
    + "<col class='secondIndicator'/>"
    + "<col class='secondRate'/>"
    + "<col class='parolRef'/>"
    + "<col class='parolText'/>"
    + "</colgroup>";
}

SecondPageView.prototype._computePageLine = function(index)
{
  var html = "";

  var lineIndex = index - this.getPageStartIndex();
  var coll = this.getCollection();
  if (!coll.length) {
    return "";
  }
  var aPair = coll[index];
  var parolQual = aPair.getSecond();

  var aParolFormatter = new ParolFormatter(this._Model);

  // Pair
  var rate = "";
  var rateTitle = "";
  var userRateTitle = "";
  var indicatorClass = "";
  if (aPair) {
    //TODO low HS 2009-12-31 - "sort by rate" in second list uses simple formula #yes-#no - improve?
    var rateValue = aPair.getYes() - aPair.getNo();
    rate = (rateValue > 0 ? "+" : "") + rateValue.toString();
    rateTitle = aPair.getYes() + " ja, " + aPair.getMaybe() + " vielleicht, " + aPair.getNo() + " nein";
    rateTitle = " title='" + rateTitle + "'";
    // show rate of user only if logged in => no need to drop data
    if (this._Model.getUserName() && aPair.getUserRate() != null) {
      var Pretty = { "-1": "nein", "0": "vielleicht", "1": "ja" };
      userRateTitle = "Ihre Bewertung: " + Pretty[aPair.getUserRate()];

      var Cls = { "-1": "no", "0": "maybe", "1": "yes" };
      indicatorClass = Cls[aPair.getUserRate()];
      // comment only if user rate given
      if (aPair.getUserComment()) {
        userRateTitle += ": " + aPair.getUserComment();
      }
      userRateTitle = " title='" + userRateTitle + "'";
    }
  }
  var aParolBox = this._Model.getParolBox();
  var aParol = aParolBox ? aParolBox.findParol(parolQual) : null;
  var ref;
  var refTitleAtt = "";
  var stateImg = "";
  var text = "";
  if (aParol) {
    var state = aParol.getCategory("State");
    var Info = aParolFormatter.getInfo(aParol) || [100];
    // in SecondList, no need to display icon for second parol
    if (Info[0] != 2 && Info[0] != 100 && Info[2]) {
      refTitleAtt = " title='" + Info[2] + "'";
      stateImg = " <img src='images/" + Info[1] + "' alt='' />";
    }

    ref = aParol.getSL();
    if (Info[3]) {
      refTitleAtt += " (" + parolQual + ")";
      ref += " (" + Info[3] + ")";
    }

    if (aParol.getHtmlIL()) {
      text = "<em>" + aParol.getHtmlIL() + "</em> ";
    }
    text += aParol.getHtmlL().replace(/\s+/g, " ");
    /* code from FilteredParolView - highlight search term in SecondList, too?
    var filter = this._Model.getFilter();
    if (filter) {
      text = text.replace(filter, "<span class='match'>" + filter + "</span>");
    }
    */
  }
  else {
    ref = parolQual;
  }
  var onclick = "document._Controller.selectSecondLine(" + lineIndex + "); return false;";
  html += "<tr class='parolRow' id='" + this._prefixForID + lineIndex + "' onclick=\"" + onclick + "\""
    + ">"
    //+ "<a href=\"javascript:" + onclick + "\">" + aParol.getSL() + "</a>"
    + "<td class='secondIndicator " + indicatorClass + "'" + userRateTitle + ">&nbsp;&nbsp;</td>"
    + "<td class='secondRate'" + rateTitle + ">" + rate + "</td>"
    + "<td class='parolRef'" + refTitleAtt + ">" + ref + stateImg + "</td>"
    + "<td class='parolText'>" + text + "</td>"
    + "</tr>\n";
  return html;
}

SecondPageView.prototype._computePageFooter = function()
{
  return "</table>";
}


