<?php
namespace App\Services;


use App\Mail\MailCheckout;
use App\Models\ShoppingOrder;
use App\Models\ShoppingPayment;
use App\User;
use Illuminate\Support\Facades\Mail;

class Payment
{

    public static $txaction_text = [
        'paid' => "bezahlt",
        'appointed' => "offen",
        'failed' => "abbruch",
        'extern' => "extern",
        'open' => "offen",
        'invoice_open' => "Re. offen",
        'invoice_paid' => "Re. bezahlt",
        'invoice_non' => "Re. keine Zahlung",
        'non' => "keine Zahlung",
        'NULL' => 'keine Zahlung',
    ];

    public static $txaction_invoice = [
        'open' => "offen",
        'paid' => "bezahlt",
        'non' => "keine Zahlung",
        /*'open_vor' => "Vorkasse offen",
        'paid_vor' => "Vorkasse bezahlt",
        'non_vor' => "Vorkasse keine Zahlung",
        'invoice_open' => "Rechung offen",
        'invoice_paid' => "Rechung bezahlt",
        'invoice_non' => 'Rechung keine Zahlung',*/

    ];

    public static $txaction_color = [
        'appointed' => "warning",
        'failed' => "danger",
        'extern' => "success",
        'open' => "warning",
        'paid' => "success",
        'non' => "danger",
        'open_vor' => "warning",
        'paid_vor' => "success",
        'non_vor' => "danger",
        'invoice_open' => "warning",
        'invoice_paid' => "success",
        'invoice_non' => "danger",

    ];


    public static function getFormattedTxaction($txaction){
        if($txaction && isset(self::$txaction_text[$txaction])){
            return self::$txaction_text[$txaction];
        }
        return self::$txaction_text['NULL'];
    }

    public static function getFormattedTxactionColor($txaction){
        if($txaction && isset(self::$txaction_color[$txaction])){
            return self::$txaction_color[$txaction];
        }
        return "warning";
    }

    public static function getShoppingOrderBadge(ShoppingOrder $shopping_order){
        if($shopping_order->mode === 'test'){
            return '<span class="badge badge-pill badge-default">'.strtoupper($shopping_order->mode).' - '.self::getFormattedTxaction($shopping_order->txaction).'</span>';
        }
        if($shopping_order->mode === 'dev'){
            return '<span class="badge badge-pill badge-info">'.strtoupper($shopping_order->mode).' - '.self::getFormattedTxaction($shopping_order->txaction).'</span>';
        }
        return '<span class="badge badge-pill badge-'.self::getFormattedTxactionColor($shopping_order->txaction).'">'.self::getFormattedTxaction($shopping_order->txaction).'</span>';
    }

    public static function getShoppingPaymentBadge(ShoppingPayment $shopping_payment){
        if($shopping_payment->mode === 'test'){
            return '<span class="badge badge-pill badge-default">'.strtoupper($shopping_payment->mode).' - '.self::getFormattedTxaction($shopping_payment->txaction).'</span>';
        }
        return '<span class="badge badge-pill badge-'.self::getFormattedTxactionColor($shopping_payment->txaction).'">'.self::getFormattedTxaction($shopping_payment->txaction).'</span>';
    }


    public static function paymentStatusPaidAction(ShoppingOrder $shopping_order, $paid){
        $send_link = false;

        $shopping_order->setUserHistoryValue(['status' => 8]);
        Shop::userOrders();
        $shopping_order->paid = $paid;
        $shopping_order->save();

        //if product has actions
        if($shopping_order->shopping_order_items && $shopping_order->auth_user_id){
            foreach($shopping_order->shopping_order_items as $shopping_order_item){
                if($shopping_order_item->product){
                    if($shopping_order_item->product->action){

                        $user = User::findOrFail($shopping_order->auth_user_id);
                        $user->save();
                        $send_link = true;
                        //new date
                        $date = \Carbon::now()->modify('1 year');
                        if($user->payment_account && $user->daysActiveAccount()>0){
                            $date = \Carbon::parse($user->payment_account)->modify('1 year');
                        }
                        foreach ($shopping_order_item->product->action as $do){
                            if($shopping_order_item->product->getActionName($do) === 'payment_for_account'){
                               // $user->payment_order_id = $shopping_order_item->product->id; //34
                                $user->payment_account = $date;
                                $user->wizard = 100;
                                $user->payment_credit = $shopping_order_item->product->price;
                                $shopping_order->setUserHistoryValue(['status' => 9]);
                            }
                            /*if($shopping_order_item->product->getActionName($do) === 'payment_for_shop'){
                                $user->payment_order_id = $shopping_order_item->product->id; //35
                                $user->payment_shop = $date;
                                $user->wizard = 100;
                                $shopping_order->setUserHistoryValue(['status' => 9]);
                            }
                            if($shopping_order_item->product->getActionName($do) === 'payment_for_shop_upgrade'){
                                if($shopping_order_item->product->upgrade_to_id){
                                    $user->payment_order_id = $shopping_order_item->product->upgrade_to_id;
                                }
                                $user->payment_shop = $user->payment_account; //same Date, is upgrade
                                $shopping_order->setUserHistoryValue(['status' => 9]);
                            }
                            if($shopping_order_item->product->getActionName($do) === 'payment_for_lead_upgrade'){
                                if($shopping_order_item->product->upgrade_to_id){
                                    $user->m_level = $shopping_order_item->product->upgrade_to_id;
                                }
                            }*/
                            $user->save();
                        }
                    }
                }

            }
        }
        //if the order has action
        if($shopping_order->shopping_user->is_from === 'user_order'){
            //is margin -> set paid
            $shopping_order->shopping_order_margin->paid = true;
            $shopping_order->shopping_order_margin->save();
            //is payment credit, reduce
            if($shopping_order->shopping_order_margin->from_payment_credit > 0){
                $new_credit = $shopping_order->auth_user->payment_credit - $shopping_order->shopping_order_margin->from_payment_credit;
                $new_credit = $new_credit < 0 ? 0 : $new_credit;
                $shopping_order->auth_user->payment_credit = $new_credit;
                $shopping_order->auth_user->save();
            }

        }


        return $send_link;
    }

    public static function paymentStatusSendMail(ShoppingOrder $shopping_order, $shopping_payment, $data){
        $bcc = [];
        $billing_email = $shopping_order->shopping_user->billing_email;
        if(!$billing_email){
            if($data['mode'] === 'test'){
                $billing_email = config('app.checkout_test_mail');
            }else{
                $billing_email = config('app.checkout_mail');
            }
        }
        if($data['mode'] === 'test'){
            $bcc[] = config('app.checkout_test_mail');
        }else{
            $bcc[] = config('app.checkout_mail');
        }

        if(!$shopping_order->shopping_user->is_like && $shopping_order->shopping_user->member){
            $bcc[] = $shopping_order->shopping_user->member->email;
        }
        Mail::to($billing_email)->bcc($bcc)->send(new MailCheckout($data['txaction'], $shopping_order, $shopping_payment, $data['send_link'], $data['mode']));
    }
}
