<commit_before><?php

/**
 * @copyright Copyright (C) 2015-2020 <NAME>
 * @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
 * @author <NAME> <<EMAIL>>
 */

declare(strict_types=1);

namespace app\components\widgets;

use Yii;
use yii\base\Widget;
use yii\helpers\Html;
use yii\i18n\Formatter;

class BattleKillDeathColumn extends Widget
{
    public ?int $kill = null;
    public ?int $death = null;
    public ?int $assist = null;
    public ?int $kill_or_assist = null;

    public ?Formatter $formatter = null;

    public function init()
    {
        parent::init();

        if ($this->formatter === null) {
            $this->formatter = Yii::$app->formatter;
        }
    }

    public function run()
    {
        return Html::tag(
            'span',
            implode(Html::tag('br'), array_filter(
                [
                    implode(' ', array_filter(
                        [
                            $this->renderValues(),
                            $this->renderLabel(),
                        ],
                        fn ($v) => $v !== null
                    )),
                    implode(' ', array_filter(
                        [
                            $this->renderKillRatio(),
                        ],
                        fn ($v) => $v !== null
                    )),
                ],
                fn ($v) => $v !== ''
            )),
            ['id' => $this->id]
        );
    }

    public function renderValues(): ?string
    {
        $kHtml = $this->renderKDValue($this->kill, Yii::t('app', 'Kills'));
        $dHtml = $this->renderKDValue($this->death, Yii::t('app', 'Deaths'));

        if ($this->kill === null) {
            if ($this->assist === null && $this->kill_or_assist === null) {
                $kPart = $kHtml; // should be "?"
            } else {
                // only "K+A" known
                $kPart = vsprintf('《%s》', [
                    $this->renderKDValue($this->kill_or_assist, Yii::t('app', 'Kill or Assist')),
                ]);
            }
        } elseif ($this->assist === null) {
            $kPart = $kHtml;
        } else {
            $kPart = vsprintf('%s (+ %s)', [
                $kHtml,
                $this->renderKDValue($this->assist, Yii::t('app', 'Assists')),
            ]);
        }

        return vsprintf('%s / %s', [
            $kPart,
            $dHtml,
        ]);
    }

    private function renderKDValue(?int $value, string $label): string
    {
        return Html::tag(
            'span',
            $value !== null
                ? $this->formatter->asInteger($value)
                : $this->formatter->asText('?'),
            [
                'class' => 'auto-tooltip',<commit_msg>Update kill/death display column style<commit_after><?php

/**
 * @copyright Copyright (C) 2015-2022 <NAME>
 * @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
 * @author <NAME> <<EMAIL>>
 */

declare(strict_types=1);

namespace app\components\widgets;

use Yii;
use yii\base\Widget;
use yii\helpers\Html;
use yii\i18n\Formatter;

final class BattleKillDeathColumn extends Widget
{
    public ?int $kill = null;
    public ?int $death = null;
    public ?int $assist = null;
    public ?int $kill_or_assist = null;

    public ?Formatter $formatter = null;

    public function init()
    {
        parent::init();

        if ($this->formatter === null) {
            $this->formatter = Yii::$app->formatter;
        }
    }

    public function run()
    {
        return Html::tag(
            'div',
            \implode('', \array_map(
                fn (string $html): string => Html::tag('div', $html),
                \array_filter(
                    [
                        \implode(' ', \array_filter(
                            [
                                $this->renderValues(),
                                $this->renderLabel(),
                            ],
                            fn (?string $v): bool => $v !== null,
                        )),
                        \implode(' ', \array_filter(
                            [
                                $this->renderKillRatio(),
                            ],
                            fn (?string $v): bool => $v !== null,
                        )),
                    ],
                    fn (string $v): bool => $v !== '',
                ),
            )),
            ['id' => $this->id],
        );
    }

    public function renderValues(): ?string
    {
        $kHtml = $this->renderKDValue($this->kill, Yii::t('app', 'Kills'));
        $dHtml = $this->renderKDValue($this->death, Yii::t('app', 'Deaths'));

        if ($this->kill === null) {
            if ($this->assist === null && $this->kill_or_assist === null) {
                $kPart = $kHtml; // should be "?"
            } else {
                // only "K+A" known
                $kPart = vsprintf('《%s》', [
                    $this->renderKDValue($this->kill_or_assist, Yii::t('app', 'Kill or Assist')),
                ]);
            }
        } elseif ($this->assist === null) {
            $kPart = $kHtml;
        } else {
            $kPart = \vsprintf('%s %s', [
                $kHtml,
                Html::tag(
                    'small',
                    \vsprintf('+ %s', [
                        $this->renderKDValue($this->assist, Yii::t('app', 'Assists')),
                    ]),
                    ['class' => 'text-muted']
                ),
            ]);

            // $kPart = vsprintf('%s (+ %s)', [
            //     $kHtml,
            //     $this->renderKDValue($this->assist, Yii::t('app', 'Assists')),
            // ]);
        }

        return vsprintf('%s / %s', [
            $kPart,
            $dHtml,
        ]);
    }

    private function renderKDValue(?int $value, string $label): string
    {
        return Html::tag(
            'span',
            $value !== null
                ? $this->formatter->asInteger($value)
                : $this->formatter->asText('?'),
            [
                'class' => 'auto-tooltip',
{'fraction_non_alphanumeric': 0.16302219321148825, 'fraction_numerical': 0.0035900783289817234, 'mean_word_length': 1.2475247524752475, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 71, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1877230', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1722, 'n_words': 336}