<?php

namespace app\common\service\qrcode;

use app\common\model\User as UserModel;
use cores\exception\BaseException;
use Exception;
use Grafika\Grafika;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use function base_url;

class InviteUser extends BaseQRcode
{
    // 当前客户端 (默认H5)
    private string $channel;

    // 用户信息
    private UserModel $userInfo;

    private int $type = 1;

    /**
     * 构造方法
     * Goods constructor.
     * @param UserModel $userInfo
     * @param string $channel 二维码渠道(小程序码、h5码)
     */
    public function __construct(UserModel $userInfo, string $channel = 'H5', int $type = 1)
    {
        parent::__construct();
        $this->type = $type;
        // 用户信息
        $this->userInfo = $userInfo;
        // 当前客户端
        $this->channel = $channel;
    }

    /**
     * 拼接海报图
     * @return string
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getImage(): string
    {
        // 判断海报图文件存在则直接返回url
        if (file_exists($this->getPosterPath())) {
            return $this->getPosterUrl();
        }
        // 商城ID
        $storeId = $this->userInfo['store_id'];
        $backdrop = __DIR__ . '/resource/invite_user.png';
        // 商品海报背景图
        if ($this->type != 1) {
            $backdrop = __DIR__ . '/resource/invite_shop.png';
        }
        // 小程序码参数
        $scene = "refereeId:" . ($this->userInfo['user_id'] ?: '');
        // 下载小程序码
        $page = 'pages/login/index';
        $qrcode = $this->getQrcode($storeId, $scene, $page, $this->channel);
        // 拼接海报图
        return $this->savePoster($backdrop, $qrcode);
    }

    /**
     * 拼接海报图
     * @param string $backdrop 背景图路径
     * @param string $qrcode 二维码图路径
     * @return string
     * @throws Exception
     */
    private function savePoster(string $backdrop, string $qrcode): string
    {
        // 实例化图像编辑器
        $editor = Grafika::createEditor(['Gd']);
        // 打开海报背景图
        $editor->open($backdropImage, $backdrop);
        // 打开小程序码
        $editor->open($qrcodeImage, $qrcode);
        // 重设小程序码宽高
        $editor->resizeExact($qrcodeImage, 140, 140);
        // 小程序码添加到背景图
        $editor->blend($backdropImage, $qrcodeImage, 'normal', 1.0, 'top-left', 520, 700);
        // 保存图片
        $editor->save($backdropImage, $this->getPosterPath());
        return $this->getPosterUrl();
    }

    /**
     * 海报图文件路径
     * @return string
     */
    private function getPosterPath(): string
    {
        // 保存路径
        $tempPath = web_path() . "temp/{$this->userInfo['store_id']}/";
        !is_dir($tempPath) && mkdir($tempPath, 0755, true);
        return $tempPath . $this->getPosterName();
    }

    /**
     * 海报图文件名称
     * @return string
     */
    private function getPosterName(): string
    {
        return 'invite_' . md5("{$this->userInfo['user_id']}_$this->channel" . $this->type) . '.png';
    }

    /**
     * 海报图url
     * @return string
     */
    private function getPosterUrl(): string
    {
        return base_url() . 'temp/' . $this->userInfo['store_id'] . '/' . $this->getPosterName() . '?t=' . time();
    }
}