<?php
/**
 * @ViewsField("basket_cart_sum")
 */

namespace Drupal\basket\Plugin\views\field;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\Core\Url;

class BasketCartSum extends FieldPluginBase{

    protected $basket;

    public function __construct(){
        call_user_func_array(array('parent', '__construct'), func_get_args());
        $this->basket = \Drupal::service('Basket');
    }

    public function query() {
        $params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : [];
        $this->query->addField('basket', 'id', 'basket_row_id', $params);
        $this->query->addField('basket', 'count', 'basket_row_count', $params);
    }
    protected function defineOptions() {
        $options = parent::defineOptions();
        $options['templatePrice'] = ['default' => '{% if discount %}
    {{ (sum-(sum/100*discount))|basket_number_format(2, \',\', \' \') }} {{ basket_t(currency) }}
{% else %}
    {{ sum|basket_number_format(2, \',\', \' \') }} {{ basket_t(currency) }}
{% endif %}'];
        return $options;
    }
    public function buildOptionsForm(&$form, FormStateInterface $form_state) {
        $form['templatePrice'] = array(
            '#type'             => 'textarea',
            '#title'            => 'Render template (Twig)',
            '#rows'             => 2,
            '#default_value'    => $this->options['templatePrice'],
            '#description'      => implode('<br/>', [
                '{{ sum }}',
                '{{ currency }}',
                '{{ discount }}'
            ])
        );
        parent::buildOptionsForm($form, $form_state);
    }
    public function render(ResultRow $values){
        if(!empty($values->basket_row_id)){
            $price = $this->basket->Cart()->getItemPrice([
                'id'        => $values->basket_row_id
            ]);
            $discount = $this->basket->Cart()->getItemDiscount([
                'id'        => $values->basket_row_id
            ]);
        }
        if(empty($price)) $price = 0;
        if(empty($discount)) $discount = 0;
        $count = !empty($values->basket_row_count) ? $values->basket_row_count : 1;
        return [
            '#type'     => 'inline_template',
            '#template' => $this->options['templatePrice'],
            '#context'  => [
                'sum'     => $price*$count,
                'currency'  => $this->basket->Cart()->getCurrencyName(),
                'discount'  => $discount
            ]
        ];
    }
}