<?php

namespace App\Http\Traits;

use Jenssegers\Agent\Agent;

trait AgentTrait
{
    // 단말기 종류 확인
    public function device(?string $useragent = null): string
    {
        return (new Agent)->device($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
    }

    // 운영체제 정보
    public function platform(?string $useragent = null): string
    {
        return (new Agent)->platform($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
    }

    // 브라우저 종류 확인
    public function browser(?string $useragent = null): string
    {
        return (new Agent)->browser($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
    }

    // 수집 로봇 이름
    public function robot(?string $useragent = null): string
    {
        return (new Agent)->robot($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
    }

    // 브라우저 버전 정보
    public function version(?string $useragent = null): string
    {
        return (new Agent)->version($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
    }

    // 브라우저 언어 정보
    public function languages(): string
    {
        return json_encode((new Agent)->languages());
    }

    // 단말기 정보
    public function deviceType(): int
    {
        if ($this->isDesktop()) {
            return DEVICE_TYPE_1; // desktop
        } else if ($this->isPhone()) {
            return DEVICE_TYPE_2; // phone
        } else if ($this->isMobile()) {
            return DEVICE_TYPE_3; // mobile
        } else if ($this->isTablet()) {
            return DEVICE_TYPE_4; // tablet
        } else if ($this->isRobot()) {
            return DEVICE_TYPE_5; // robot
        }
        return DEVICE_TYPE_0; // ALL
    }

    public function isDesktop()
    {
        return (new Agent)->isDesktop();
    }

    public function isMobile()
    {
        return (new Agent)->isMobile();
    }

    public function isPhone()
    {
        return (new Agent)->isPhone();
    }

    public function isTablet()
    {
        return (new Agent)->isTablet();
    }

    public function isRobot()
    {
        return (new Agent)->isRobot();
    }
}
