import drawText from '../util/drawText.js'
import drawImage from '../util/drawImage.js'
import Modal from './Modal'
import { SetPriceBtnArea, getBaseCanvas, getBaseContext } from '../conf/data.js'

let modal_center
export default class PriceModal extends Modal {
  constructor(context, data) {
    super()
    this.bg_pop_win = 'images/agent/bg_price@2x.png'
    this.canvas = getBaseCanvas()
    this.context = getBaseContext()
    this.data = data
  }
  drawContent = () => {
    let ctx = this.context
    let next
    let startX = this.win_dimension.startX;
    let startY = 160 + 76;
    modal_center = startX + this.win_dimension.width / 2
    let promises = []
    promises.push(drawText(ctx, this.data.name, modal_center, startY + font_size * 3, 'center'))
    promises.push(drawText(ctx, '¥' + (this.data.price || '0'), modal_center, startY + font_size * 5, 'center').then((dimension) => {
      this.price_dimension = SetPriceBtnArea[0] = {...dimension}
      return dimension
    }))
    promises.push(drawImage(ctx, 'images/agent/btn_save@2x.png', modal_center, startY + font_size * 7, 'center,top').then((dimension) => {
      SetPriceBtnArea[1] = {...dimension}
      return dimension
    }))
    return Promise.all(promises)
  }
  updatePrice(price) {
    let price_m = this.price_dimension
    this.context.fillStyle = '#fdedac'
    this.context.fillRect(price_m.startX, price_m.startY, price_m.width, price_m.height)
    return drawText(this.context, '¥' + (price || '0'), modal_center, price_m.startY, 'center').then((dimension) => {
      price_m = SetPriceBtnArea[0] = { ...dimension }
      GameGlobal.canvas.getContext('2d').drawImage(this.canvas, 0, 0)
      return dimension
    })
  }
}