<?php

namespace Buckhill\Yubico\Modes\OATH;


class OTP
{
    protected $otp;
    protected $parsed;

    public function __construct($otp)
    {
        $this->otp = $otp;

        $this->parse();
    }


    /**
     * @return int
     */
    public function getOTP()
    {
        return $this->parsed['otp'];
    }


    /**
     * @return string
     */
    public function getTokenIdentifier()
    {
        return sprintf("%s%s%s", $this->parsed['omp'], $this->parsed['tt'], $this->parsed['token_id']);
    }

    /**
     * @return $this
     */
    protected function parse()
    {
        $result = [
            'omp'   => null,
            'tt'    => null,
            'token_id' => null,
            'otp' => ''
        ];

        // In this case, no additional info has been included in the otp
        if((strlen($this->otp) === 6) || (strlen($this->otp) === 8))
        {
            $result['otp'] = $this->otp;

            return $result;
        }

        $result = [
            'omp'           => substr($this->otp, 0, 2),
            'tt'            => substr($this->otp, 2, 2),
            'token_id'      => substr($this->otp, 4, 8),
            'otp'           => substr($this->otp, 12)
        ];

        $this->parsed = $result;

        return $this;
    }
}