<?php

namespace App\Services\NoticeService\Channels;

use App\Http\Controllers\Miniprogram\Core;

class MiniTemplateChannel extends Channel
{

    public $uuid;
    public $notice_type = "小程序模板消息";
    protected $url;
    protected $page_path;
    protected $data;

    public function __construct(int $uid, string $templateId, array $data)
    {
        $this->uid = $uid;
        $this->templateId = $templateId;
        $this->data = $data;
        $this->content = json_encode($data, JSON_UNESCAPED_UNICODE);
    }


    public function setUuid($uuid)
    {
        $this->uuid = $uuid;

        return $this;
    }


    public function setPath($path)
    {
        $path = urlencode("/" . $path);
        $page_path = "pages/starter/starter?log_type=notice&log_id={$this->uuid}&launch_type=free&url={$path}";
        $this->page_path = $page_path;

        return $this;
    }


    /**
     * @return bool
     */
    public function send(): bool
    {
        $publicId = config('miniprogram.public_id');
        $page = $this->page_path;
        $data = $this->data;
        $core = new Core();

        return $core->miniTemplate($this->uid, $this->templateId, $publicId, $page, $data);
    }
}
