var Sprite = require('./libs/sprite.js').Sprite
var Label = require('./libs/label.js').Label

function RankItem(x, y, width, height) {
  this.x = x;
  this.y = y;
  this.width = width;
  this.height = height;
}

RankItem.prototype.create = function() {
  let labelOffset = 55;
  let labelStyle = "#ffffff";
  let fontSize = "35px";

  // this.rankbg = new Sprite("openDataContext/asset/paiwei_xin_1_6_1.png",this.x, this.y + this.height * 0.5 - 150 * 0.5, 650, 140);
  // this.rankbg.parent = this;

  this.rankLabel = new Label(this.x + 100, this.y + labelOffset+5);
  this.rankLabel.fontSize = "40px";
  this.rankLabel.fillStyle = labelStyle;
  this.rankLabel.textAlign = 'center';
  this.rankLabel.fontFamily = "Microsoft YaHei";

  let rankWidth = 38;
  let rankHeight = 48;
  this.rankimg = new Sprite('', this.x + 80, this.y+25/** + this.height * 0.5 - rankHeight * 0.5*/, rankWidth, rankHeight);

  let offsetX = 75;
  let iconWidth = 56;
  let iconHeight = 56;
  this.icon = new Sprite('', this.x + offsetX + 50, this.y+20 /**+ this.height * 0.5 - iconHeight * 0.5*/, iconWidth, iconHeight);

  this.nameLabel = new Label(this.x + 190, this.y + labelOffset);
  this.nameLabel.fontSize = fontSize;
  this.nameLabel.fillStyle = labelStyle;
  this.nameLabel.fontFamily = "Microsoft YaHei";

  this.scoreNum = new Label(this.x + this.width - 160, this.y + 55);
  this.scoreNum.fontSize = "35px";
  this.scoreNum.fillStyle = labelStyle;
  this.scoreNum.textAlign = "center"
  this.scoreNum.fontFamily = "Microsoft YaHei";

  this.scoreLabel = new Label(this.x + this.width - 100, this.y + labelOffset);
  this.scoreLabel.fontSize = fontSize;
  this.scoreLabel.fillStyle = labelStyle;
  this.scoreLabel.textAlign = "right"
  this.scoreLabel.fontFamily = "Microsoft YaHei";
}

RankItem.prototype.getBgFillStyle = function(num) {
  let rankNum = num;
  let style = "#ffffff";
  if (num % 2 == 1) {
    style = "#ffffff";
  }
  return style;
}

var rank1url = "BtmopenDataContext/asset/paihang-1-3.png";
var rank2url = "BtmopenDataContext/asset/paihang-1-4.png";
var rank3url = "BtmopenDataContext/asset/paihang-1-5.png";
var bgImg = "openDataContext/asset/bg.png";
RankItem.prototype.getRankUrl = function(num) {
  let rankNum = num;
  let url = "";
  switch (rankNum) {

    case 1:
      url = rank1url;
      break;
    case 2:
      url = rank2url;
      break;
    case 3:
      url = rank3url;
      break;
    default:
      url = "";
      break;
  }
  return url;
}

RankItem.prototype.setData = function(rankNum, iconUrl, name, score, keyName) {

  this.icon.src = iconUrl;
  this.nameLabel.text = name;
  this.scoreNum.text = score;
  this.scoreLabel.text ="第     关";
  this.rankLabel.text = rankNum;
  if (rankNum <= 3) {
    this.rankLabel.visible = false;
    this.rankimg.visible = true;
    this.rankimg.src = this.getRankUrl(rankNum);
  } else {
    this.rankLabel.visible = true;
    this.rankimg.visible = false;
    this.rankLabel.text = rankNum;
  }
}

RankItem.prototype.position = function(x, y) {
  this.x = x;
  this.y = y;

  let labelOffset = 80;
  this.rankLabel.position(this.x + 50, this.y + labelOffset);

  let rankWidth = 51;
  let rankHeight = 73;
  this.rankimg.position(this.x + 30, this.y )//+ this.height * 0.5 - rankHeight * 0.5);

  let offsetX = 90;
  let iconWidth = 60;
  let iconHeight = 60;
  this.icon.position(this.x + offsetX + 30, this.y-10)// + this.height * 0.5 - iconHeight * 0.5);
  this.nameLabel.position(this.x + 110, this.y + labelOffset);

  this.scoreLabel.position(this.x + this.width - 50, this.y + labelOffset);
}

RankItem.prototype.drawToCanvas = function(ctx) {
  this.rankLabel.drawToCanvas(ctx);
  this.rankimg.drawToCanvas(ctx);
  this.scoreNum.drawToCanvas(ctx);
  this.icon.drawToCanvas(ctx);
  this.nameLabel.drawToCanvas(ctx);
  this.scoreLabel.drawToCanvas(ctx);
}

module.exports.RankItem = RankItem;