<?php

namespace KirilKirkov\WpInvoices;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class SendInvoice
{
    private static $validation_errors = [];
    private static $send_to = [];

    /**
     * Send invoice by email
     * HAS NONCE VALIDATION OUTSIDE (INTO CALLING FUNCTION) !
     * 
     * @param array $post - submit post email
     * @param array $invoice - invoice from db, validated in Invoices class
     * 
     * @return bool
     */
    public static function send($post, $invoice)
    {
        // validate
        if(!self::validate($post)) {
            return false;
        }

        $output_file = WpInvoicesPdf::generate_pdf_file($invoice);

        $error = '';

        $headers = array('Content-Type: text/html; charset=UTF-8');
        $attachments = [$output_file];

        $r = wp_mail(implode(',', self::$send_to), $post[Config::INPUTS_PREFIX.'send_invoice_subject'], $post[Config::INPUTS_PREFIX.'send_invoice_body'], $headers, $attachments);
                
        // set error or mark invoice as sended
        if($r !== true) {
            if($error === '') {
                $error = esc_html__('Undefined error. Please use the custom SMTP Server settings.', 'kirilkirkov-pdf-invoice-manager');
            }
            self::set_validation_error(Config::INPUTS_PREFIX.'send_invoice_mail_error', $error);
        } else {
            Invoices::mark_invoice_as_sended($invoice['id']);
        }

        // clear tmp
        wp_delete_file($output_file);

        return $r;
    }

    private static function validate($post)
    {
        $send_to = [];
        if(!isset($post[Config::INPUTS_PREFIX.'send_invoice_emails']) || trim($post[Config::INPUTS_PREFIX.'send_invoice_emails']) === '') {
            self::set_validation_error(Config::INPUTS_PREFIX.'send_invoice_emails', esc_html__('Please enter emails', 'kirilkirkov-pdf-invoice-manager'));
        } else {
            $send_to = explode(',', $post[Config::INPUTS_PREFIX.'send_invoice_emails']);
            foreach($send_to as $k => &$e) {
                if(is_email($e)) {
                    $e = sanitize_email($e);
                    continue;
                }
                unset($send_to[$k]);
            }
        }

        // after emails filter
        if(!count($send_to)) {
            self::set_validation_error(Config::INPUTS_PREFIX.'send_invoice_emails', esc_html__('Please enter valid emails', 'kirilkirkov-pdf-invoice-manager'));
        } else {
            self::$send_to = $send_to;
        }

        if(!isset($post[Config::INPUTS_PREFIX.'send_invoice_subject']) || trim($post[Config::INPUTS_PREFIX.'send_invoice_subject']) === '') {
            self::set_validation_error(Config::INPUTS_PREFIX.'send_invoice_subject', esc_html__('Please enter subject', 'kirilkirkov-pdf-invoice-manager'));
        }

        if(!isset($post[Config::INPUTS_PREFIX.'send_invoice_body']) || trim($post[Config::INPUTS_PREFIX.'send_invoice_body']) === '') {
            self::set_validation_error(Config::INPUTS_PREFIX.'send_invoice_body', esc_html__('Please enter body', 'kirilkirkov-pdf-invoice-manager'));
        }

        return (bool)!count(self::get_validation_errors());
    }

    /**
     * Set error to validation errors
     */
    private static function set_validation_error($key, $err)
    {
        self::$validation_errors[$key] = $err;
    }

    /**
     * @return array
     */
    public static function get_validation_errors()
    {
        return self::$validation_errors;
    }
}