<?php

namespace App\Listeners;

use App\Models\Setting;
use App\Events\MessageAllocationEvent;
use Illuminate\Queue\InteractsWithQueue;

class MessageAllocationListener
{
    use InteractsWithQueue;

    //站点 token:
    public $token = '';

    /**
     * Create the event listener.
     *
     * @param Setting $setting
     */
    public function __construct(Setting $setting)
    {
        $this->token = $setting->get('apptoken');
    }

    /**
     * Handle the event.
     *
     * @param  MessageAllocationEvent $event
     * @return void
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function handle(MessageAllocationEvent $event)
    {
        $message = $event->message;

        if ($this->token) {
            $email = $message->user->email ?? null;
            $data = [];

            if ($email) {
                $data['form_params']['email'] = [
                    'subject' => 'You have new inquiry allocation information！',
                    'email'   => $email,
                    'body'    => $this->emailContent($message),
                    'mail_address' => Setting::get('mail_address'),
                    'mail_password' => Setting::get('mail_password'),
                    'mail_name' => Setting::get('mail_name'),
                    'mail_host' => Setting::get('mail_host')
                ];
            }

            if (!empty($data)) {
                $client = new \GuzzleHttp\Client(['timeout'  => 5.0]);

                $data['headers'] = [
                    'x-requested-with' => 'XMLHttpRequest',
                    'token' => $this->token,
                ];

                try {
                    $response = $client->request('POST', config('setting.api_url').'/sms/send', $data);
                    if ($response->getStatusCode() == 200) {
                        logger('message allocation log: '.$response->getBody()->getContents());
                    }
                } catch (\Exception $e) {
                    logger('exception message allocation log: '.$e->getMessage());
                }
            }
        }
    }

    private function emailContent($obj)
    {
        $content = "<div class='content'>";
        $content .= "<div class='titles'><strong>Subject: </strong>{$obj->subject}</div>";
        $content .= "<div class='allocation'><span><strong>Name: </strong>{$obj->name}</span><span><strong>Phone: </strong>{$obj->phone}</span></div>";
        $content .= "<div class='allocation'><span><strong>Email: </strong>{$obj->email}</span><span><strong>Company: </strong>{$obj->company}</span></div>";
        $content .= "<div class='allocation'><strong>Content: </strong>{$obj->content}</div>";
        $content .= "</div>";

        return $content . $this->contentStyle();
    }

    private function contentStyle()
    {
        $style = "<style>";
        $style .= ".content div {padding-bottom: 10px;}";
        $style .= ".titles {font-size: 16px;line-height: 36px;}";
        $style .= ".allocation {font-size: 14px;line-height: 24px;}";
        $style .= ".allocation span {display: inline-block; width: 360px;padding-right: 10px;}";
        $style .= "</style>";

        return $style;
    }
}
