import drawText from '../util/drawText.js'
import drawImage, { drawScaleImage } from '../util/drawImage.js'
import Modal from './Modal'
import { getProductCanvas, getProductContext } from '../conf/data.js'

export default class ContactModal extends Modal {
  constructor(context, seller_name = '', seller_phone = '', seller_addr = '') {
    super()
    if (screenWidth < 1334) {
      this.scale = 0.8;
    }
    this.bg_pop_win = 'images/contact_popup/bg_win@2x.png'
    this.canvas = getProductCanvas()
    this.context = getProductContext()
    this.seller_name = seller_name
    this.seller_phone = seller_phone
    this.seller_phone = seller_phone
    this.seller_addr = seller_addr
  }
  drawContent = () => {
    let ctx = this.context
    let win_d = this.win_dimension

    return drawScaleImage(ctx, 'images/contact_popup/h_line@2x.png', screenWidth / 2, win_d.startY + win_d.height * 0.63, 'center', this.scale).then((dimension) => {
      return drawScaleImage(ctx, 'images/contact_popup/avatar@2x.png', dimension.startX, win_d.startY + win_d.height * 0.39, 'middle', this.scale).then((a_dimension) => {
        // 信息
        let middle = a_dimension.startY + a_dimension.height / 2
        let promises = []
        promises.push(drawText(ctx, '姓名   ' + this.seller_name, a_dimension.endX + font_size, a_dimension.startY + font_size / 2))
        promises.push(drawText(ctx, '电话   ' + this.seller_phone, a_dimension.endX + font_size, a_dimension.endY - font_size / 2, 'bottom'))
        promises.push(drawText(ctx, '联系', dimension.endX - font_size, middle - 2, 'right bottom'))
        promises.push(drawText(ctx, '商家', dimension.endX - font_size, middle + 2, 'right top').then((t_dimension) => {
          return drawScaleImage(ctx, 'images/contact_popup/icon_phone@2x.png', t_dimension.startX - font_size * 3, middle, 'right,middle', this.scale)
        }))
        //地址
        promises.push(drawScaleImage(ctx, 'images/contact_popup/icon_addr@2x.png', a_dimension.startX + a_dimension.width / 2, win_d.startY + win_d.height * 0.8, 'center,middle', this.scale).then((l_dimension) => {
          var middle = l_dimension.startY + l_dimension.height / 2

          let promises = []
          promises.push(drawText(ctx, '地址：   ' + this.seller_addr, a_dimension.endX + font_size, middle, 'middle'))
          promises.push(drawText(ctx, '路线', dimension.endX - font_size, middle - 2, 'right bottom'))
          promises.push(drawText(ctx, '导航', dimension.endX - font_size, middle + 2, 'right top').then((t_dimension) => {
            return drawScaleImage(ctx, 'images/contact_popup/icon_location@2x.png', t_dimension.startX - font_size * 3, middle, 'right,middle', this.scale)

          }))
          return Promise.all(promises)
        }))
        return Promise.all(promises)
      })
    })


  }
}