import { useWatermarkStore } from '@/common'
import { showCurrentTime } from '@/common/libraries/day'
import { showToast } from '@/common/libraries/naviHelper'
import { uploadImgWatermark } from '@/common/libraries/upload'
import useModel from './model'
const watermark = useWatermarkStore()

export default {
  handleWatermarkCameraSwitch() {
    useModel.devicePosition.value = useModel.devicePosition.value === 'back' ? 'front' : 'back'
  },

  takePhoto() {
    const ctx = uni.createCameraContext()
    const systemInfo = uni.getSystemInfoSync()
    let canvasTop = systemInfo?.windowHeight * (82 / 100)
    console.log(systemInfo, 'systemInfo')
    ctx.takePhoto({
      quality: 'high',
      success: (res: any) => {
        uni.showLoading({
          title: useModel?.source?.value === 'save' ? '正在保存...' : '正在上传...',
          mask: true
        })
        let ctx = uni.createCanvasContext('watermark_camera')
        uni.getImageInfo({
          src: res?.tempImagePath,
          success: img => {
            const { width, height } = img
            console.log(img, 'img')
            const canvasWidth = 430
            const canvasHeight = (height * canvasWidth) / width
            useModel.time.value = showCurrentTime()
            ctx.drawImage(res?.tempImagePath || '', 0, 0, canvasWidth, canvasHeight) // 这里设置绘制的大小
            ctx.setFillStyle('rgba(0, 0, 0, 0.2)')
            ctx.fillRect(0, canvasTop - 100, canvasWidth, 100)
            ctx.setFillStyle('#ff7105')
            ctx.fillRect(131, canvasTop - 75, 2, 28)
            ctx.fillStyle = '#fff'
            ctx.font = '500 16px Arial'
            ctx.fillText(useModel?.address?.value?.address || useModel?.address?.value?.formatted_address || '', 15, canvasTop - 17) // 地址
            ctx.fillText(useModel?.userInfo?.value?.name || '没有登录', 141, canvasTop - 64) // 日期
            ctx.font = '500 12px Arial'
            ctx.fillText(
              `${useModel?.day.value || ''} ${useModel?.weekDay.value || ''}`,
              141,
              canvasTop - 49
            ) // 周几
            ctx.setLineWidth(3)
            ctx.setStrokeStyle('#fff')
            ctx.font = '900 45px Arial'
            ctx.fillText(useModel?.time.value || '', 12, canvasTop - 45) // 时间
            ctx.strokeText(useModel?.time.value || '', 12, canvasTop - 45) // 时间
            ctx.stroke()
            ctx.draw(false, () => {
              uni.canvasToTempFilePath({
                canvasId: 'watermark_camera',
                success: nImg => {
                  console.log(nImg, 'nImg')
                  if (useModel?.source?.value === 'current') {
                    //打卡进来
                    uploadImgWatermark({
                      path: nImg?.tempFilePath,
                      name: `${useModel?.userInfo?.value?.name || '未登录'}-打卡-${useModel?.day.value || ''}`
                    })
                      .then(uploadRes => {
                        console.log(uploadRes, 'uploadRes')
                        watermark.watermarkInfo.value = uploadRes ? [uploadRes] : []
                        uni.navigateBack({
                          delta: 1
                        })
                        uni.hideLoading()
                      })
                      .catch(err => {
                        console.log(err, 'err')
                        uni.hideLoading()
                      })
                  }
                  if (useModel?.source?.value === 'save') {
                    //首页水印相机
                    uni.saveImageToPhotosAlbum({
                      filePath: nImg?.tempFilePath,
                      success: function () {
                        showToast('保存成功')
                        console.log('save success')
                      }
                    })
                  }
                }
              })
            })
          }
        })

        console.log(res)
      }
    })
  }
}
