<?php

namespace kernel\app_modules\card\services;

use DragonCode\CardNumber\CardNumber;
use DragonCode\CardNumber\Factories\BankFactory;
use DragonCode\CardNumber\Formatters\BankFormatter;
use kernel\helpers\Debug;
use kernel\app_modules\card\models\Card;
use kernel\FormModel;
use kernel\helpers\ImageGD;
use kernel\modules\user\models\User;

class CardService
{
    protected CardFileService $cardFileService;

    public function __construct()
    {
        $this->cardFileService = new CardFileService();
    }

    public function create(FormModel $form_model): false|Card
    {
        $model = new Card();
        // Пример заполнения:
        $model->user_id = $form_model->getItem('user_id');
        $model->payment_type = $form_model->getItem('payment_type') ?? 2;
        $model->bank_id = $form_model->getItem('bank_id') ?? 232;
        $model->info = $form_model->getItem('info') ?? 42;
        $model->program = $form_model->getItem('program') ?? 74;
        $model->balance = $form_model->getItem('balance') ?? 0;
        $model->cvc = $form_model->getItem('cvc');
        $model->pin = $form_model->getItem('pin');
        $model->username = $form_model->getItem('username');
        $model->card_template_id = $form_model->getItem('card_template_id');
        $model->status = $form_model->getItem('status');

        if ($model->save()) {
            $cardFile = $this->cardFileService->create($model);
            if ($cardFile) {
                $model->card_file_id = $cardFile->id;
                $model->save();
            }

            return $model;
        }

        return false;
    }

    public function update(FormModel $form_model, Card $card): false|Card
    {
        // Пример обновления:
        $card->user_id = $form_model->getItem('user_id');
        $card->payment_type = $form_model->getItem('payment_type');
        $card->bank_id = $form_model->getItem('bank_id');
        $card->info = $form_model->getItem('info');
        $card->program = $form_model->getItem('program');
        $card->balance = $form_model->getItem('balance');
        $card->cvc = $form_model->getItem('cvc');
        $card->pin = $form_model->getItem('pin');
        $card->username = $form_model->getItem('username');
        $card->status = $form_model->getItem('status');

        if ($card->card_template_id !== (int)$form_model->getItem('card_template_id')) {
            $card->card_template_id = $form_model->getItem('card_template_id');
            $cardFile = $this->cardFileService->create($card);
            if ($cardFile) {
                $card->card_file_id = $cardFile->id;
            }
        }

        if ($card->save()) {
            return $card;
        }

        return false;
    }

    public static function createCardPNG(Card $card): false|string
    {
        if ($card->cardTemplate) {
            $formatter = BankFormatter::create();

            $customer = BankFactory::create()->paymentType($card->payment_type)->bank($card->bank_id, $card->info, $card->program)->client($card->id);

            $cardNumber = CardNumber::generate($customer, $formatter);
            //Card
            $img = ROOT_DIR . "/" . $card->cardTemplate->path; // Ссылка на файл
            $font = RESOURCES_DIR . "/tmp/arialmt.ttf"; // Ссылка на шрифт

            $img = new ImageGD($img);

            $pngFilePath = RESOURCES_DIR . "/tmp/" . $card->id . ".png";
            $pngFileUrl = "/resources/tmp/" . $card->id . ".png";

            $img->addText(font_size: 14, degree: 0, x: 15, y: 25, color: "#000000", font: $font, text: "KO coin");
            $img->addText(font_size: 14, degree: 0, x: 15, y: 45, color: "#000000", font: $font, text: "BGroup\ITGuild");
            $img->addText(font_size: 18, degree: 0, x: 15, y: 180, color: "#000000", font: $font, text: $cardNumber);
            $img->addText(font_size: 12, degree: 0, x: 15, y: 200, color: "#000000", font: $font, text: $card->username);
            $img->save($pngFilePath);

            return $pngFileUrl;
        }

        return false;
    }

    public static function userHasCard(int $userId): bool
    {
        $card = Card::where("user_id", $userId)->first();
        if ($card){
            return true;
        }

        return false;
    }
}