<?php

namespace App\Http\Controllers;

use App\Adapters\Payments\Payment as AdapterPayment;
use App\Product;
use App\Cart;
use Illuminate\Http\Request;
use App\Order;
use App\Coupon;
use App\OrderItem;
use Response;

class PaymentController extends Controller
{
    public function checkPayment(){
        $cart = Cart::where('id', $_COOKIE['cartID'])->first();
        return view('shop.checkout',compact('cart'));
    }

    private function createOrder($request){
        $lastOrder = Order::orderBy('id','DESC')->first();
        if($lastOrder->order_number){
            $orderNumber = $lastOrder->order_number;
            $orderNumber++;
        }else{
            $orderNumber = 'SHOP-0000000001';
        }

        $request['customer_phone'] = $request->customer_phone_number;
        $request['order_number'] = $orderNumber;
        $orderID = Order::insertGetId($request->except('_token','customer_phone_number'));
        $items = Product::getCart()->cartItems;

        Order::find($orderID)->update([
            'base_total' => getCartTotal(),
            'shipping_ammount' => getDeliveryPrice(),
            'grand_total' => getOrderTotal(),
            'payment_status' => 'non-paid',
        ]);
        foreach($items as $item){
            OrderItem::create([
                'product_id' => $item->product_id,
                'qty_ordered' => $item->quantity,
                'order_id' => $orderID
            ]);
        }

        return $orderID;
    }

    private function payOrder($orderID){
        $order = Order::find($orderID);
        $adapter = AdapterPayment::factory(
            'Monri',
            [
                'order' => $order,
            ]
        );
       // $adapter->setSandboxMode($this->_sandbox);
        $total = getOrderTotal() * 100;
        $adapter->setAmount($total);
        $adapter->setItems();

        //if transaction is inserted, make form
        $adapter->initPayment();
    }

    public function finishOrder(Request $request){
        $orderID = $this->createOrder($request);
        if($request->payment_method == 'monri'){
            return $this->payOrder($orderID);
        }else{
            return view('shop.orders.finished_order');
        }
    }

    /**
     * function coupon usage for discount
     * function used at checkout form
     */
    /*TODO postaviti uslov o provjeri trajanja kupona */
    public function useCoupon(Request $request){
        $data = [];
        $coupon = $request->coupon;
        $coupon = Coupon::where('coupon',$coupon)->first();
        if($coupon && $coupon->usage_limit>0){
            $discount = $coupon->discount;
             Cart::where('id',$_COOKIE['cartID'])->first()->update([
                'discount' => $discount
            ]);
            $coupon->usage_limit -=1;
            $coupon->times_used +=1;
            $coupon->save();
            $data['success'] = true;
            $data['discount'] = $discount;
        }else{
            $data['success'] = false;
            $data['errors']  = 'Kupon koji ste unijeli nije validan';
        }

        return Response::json($data);
    }
}
