<?php
/**
 * Created by PhpStorm.
 * User: merdan
 * Date: 9/24/2021
 * Time: 17:16
 */

namespace TPS\Payment\Http\Controllers;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Support\Facades\Log;
use Webkul\Checkout\Facades\Cart;
use TPS\Payment\Methods\TFEB;
use Webkul\Checkout\Repositories\CartRepository;
use Webkul\Sales\Repositories\OrderRepository;


class TFEBController extends Controller
{

    public function __construct(protected OrderRepository $orderRepository, protected TFEB $teb)
    {
    }

    /**
     * Redirects to payment gateway
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function redirect(){
        // register order to payment gateway
        if( $result = $this->teb->registerOrder()){
            return redirect($result['url']);
        }

        session()->flash('error', $result['message']);

        return redirect()->route('shop.checkout.onepage.index');
    }

    /**
     * Success payment
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function complete(CartRepository $cartRepository,$cart_id){

        if(!auth()->guard()->check() && !session()->has('cart')){
            Cart::setCart($cartRepository->find($cart_id));
        }

        $result = $this->teb->getOrderStatus();

        if(!auth()->guard()->check() && !session()->has('cart')){
            Cart::setCart($cartRepository->find($cart_id));
        }

        $result = $this->senagat->getOrderStatus();

        if ($result['success'] ) {
            $order = $this->orderRepository->create(Cart::prepareDataForOrder());

            Cart::deActivateCart();

            session()->flash('order', $order);

//            return view('shop::checkout.retry',$result);

            return redirect()->route('shop.checkout.success');
        }

        return view('shop::checkout.retry',$result);
    }



}