<?php

namespace GriffinForms\Admin\Html\Elements;

class itemHistory extends \GriffinForms\Admin\Html\Format
{
    protected $lang;
    protected $item;
    protected $format;
    protected $author;
    protected $last_editor;
    protected $created;
    protected $last_edited;
    
    protected function setFormat()
    {
        $this->format = $this->config->getDateTimeFormat();
    }
    
    protected function isUserSet($role)
    {
        $user_id = $this->item->getProp($role);
        $this->$role = get_user_by('id', $user_id);
        if (is_object($this->$role)) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isDateSet($event)
    {
        $created_on = $this->item->getProp($event);
        if ($created_on != '0000-00-00 00:00:00' and $created_on != '') {
            $this->$event = wp_date($this->format, strtotime($created_on));
            return true;
        } else {
            return false;
        }
    }
    
    protected function createText()
    {
        $text = '';
        if ($this->isUserSet('author') and $this->isDateSet('created')) {
            $text = $this->lang->itemCreated($this->author->display_name, $this->created);
        }
        
        return $text;
    }
    
    protected function editText()
    {
        $text = '';
        if ($this->isUserSet('last_editor') and $this->isDateSet('last_edited')) {
            $text = $this->lang->itemEdited($this->last_editor->display_name, $this->last_edited);
        }
        
        return $text;
        
    }
    
    public function getEditText() {
        return $this->editText();
    }

    protected function renderCreateHistory()
    {
        if (!($this->isUserSet('author') and $this->isDateSet('created'))) {
            return esc_html($this->createText());
        }

        $avatar = get_avatar($this->author->user_email, 18, 'retro');

        ob_start();
        echo '<span class="gf-item-history__text">' . esc_html__('Created by', 'griffinforms-form-builder') . '</span> ';
        echo '<span class="gf-item-history__user">';
        echo wp_kses_post($avatar);
        echo '<span class="gf-item-history__user-name">' . esc_html($this->author->display_name) . '</span>';
        echo '</span> ';
        echo '<span class="gf-item-history__text">' . sprintf(esc_html__('on %s.', 'griffinforms-form-builder'), $this->created) . '</span>';
        return trim((string) ob_get_clean());
    }
    
    public function html()
    {
        $create_history = $this->renderCreateHistory();
        $edit_history = $this->editText();

        if ($create_history === '' && $edit_history === '') {
            return;
        }

        echo '<div class="griffinforms-item-history row small">';
        echo '<div class="col-md-12 d-flex">';
        if ($create_history !== '') {
            echo '<span id="griffinforms-item-create-history" class="me-1">' . $create_history . '</span>';
        }
        if ($edit_history !== '') {
            echo '<span id="griffinforms-item-edit-history">' . esc_html($edit_history) . '</span>';
        }
        echo '</div></div>';
        
    }
    
    public function __construct($lang, $item)
    {
        $this->lang = $lang;
        $this->item = $item;
        $this->setAll();
    }
}
