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 = 62;
  let labelStyle = "#3C8E39FF";
  let fontSize = "35px";

  // if(this.id%2 == 0)
  // {
  //   this.rankbg = new Sprite("openDataContext/asset/paihang_1_4.png",this.x, this.y + this.height * 0.5 - 150 * 0.5, 650, 140);
  //   this.rankbg.parent = this;
  // }

  this.rankLabel = new Label(this.x + 75, this.y + labelOffset);
  this.rankLabel.fontSize = "55px";
  this.rankLabel.fillStyle = labelStyle;
  this.rankLabel.textAlign = 'center';
  this.rankLabel.font = "Microsoft YaHei";

  let rankWidth = 67;
  let rankHeight = 63;
  this.rankimg = new Sprite('openDataContext/asset/paihang_1_6_1.png', this.x + 20, this.y + this.height * 0.5 - rankHeight * 0.5, rankWidth, rankHeight);

  let offsetX = 90;
  let iconWidth = 76;
  let iconHeight = 76;
  this.icon = new Sprite('', this.x + offsetX + 30, this.y + this.height * 0.5 - iconHeight * 0.5, iconWidth, iconHeight);

  this.nameLabel = new Label(this.x + 210, this.y + labelOffset);
  this.nameLabel.fontSize = fontSize;
  this.nameLabel.fillStyle = labelStyle;
  this.nameLabel.font = "Microsoft YaHei";
  this.nameLabel.textcolor = "000000FF";

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

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

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

var rank1url = "openDataContext/asset/rank1.png";
var rank2url = "openDataContext/asset/rank2.png";
var rank3url = "openDataContext/asset/rank3.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 + 45, this.y + labelOffset);

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

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

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

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

module.exports.RankItem = RankItem;