<?php

///* GenericListItem
// returns a List Item generated from a PhonalisaRecord Model
require_once "GenericListItem.php";

class GenericList extends IppList
{

    public $phone;
    public $screen;
    public $listType;
    public $columns;
    public $uri;

    public function __construct($screen, $listType, $columns, $uri, $listTitle, $type = IppList::TypeImplicit)
    {
        parent::__construct($type, $columns);
        $this->phone = new IppPhone();
        $this->screen = $screen;

        $this->setTitle($listTitle);
        $this->setUrl($uri);

        $this->type = $listType;
        $this->columns = $columns;
        $this->uri = $uri;
        $this->title = $listTitle;
    }

    public function addItem(GenericListItem $item, $selected = Option::NoneSelected)
    {
        $option = new Option($selected, $item->key, $item->value);
        $optionText = new OptionText($item->title, $this->uri, $item->key . "=" . $item->value);
        $option->setText($optionText);
        if ($item->imagePath) {

            $hostInfo = app()->request->getHostInfo();
            $image = new Image($hostInfo . $item->imagePath);
            $option->addUIElement($image);
        }
        $this->addOption($option);
    }

    public function addCustomItem($label, $name, $value, $imagePath, $selected = Option::NoneSelected)
    {
        $option = new Option($selected, $name, $value);
        $option->setText(new OptionText($label));
        if ($imagePath) {
            $image = new Image($imagePath);
            $option->addUIElement($image);
        }
        $this->addOption($option);
    }

    public function printXml()
    {
        $this->screen->addUIElement($this);
        $this->phone->addScreen($this->screen);
        $this->phone->printScreen();
    }
}
