<?php

namespace App\ThirdParty\Biteship;

use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;

class Order
{
    public function byPostal($params)
    {
        $url = env("BITESHIP_URL");
        $key = env("BITESHIP_KEY");
        $res = Http::withHeaders([
                "authorization" => $key
            ])
            ->withBody(json_encode([
                "origin_contact_name" => $params["origin_contact_name"],
                "origin_contact_phone" => $params["origin_contact_phone"],
                "origin_address" =>  $params["origin_address"],
                "origin_postal_code" => $params["origin_postal_code"],
                "origin_coordinate" => [
                    "latitude" => $params["origin_latitude"],
                    "longitude" => $params["origin_longitude"],
                ],

                "destination_contact_name" => $params["destination_contact_name"],
                "destination_contact_phone" => $params["destination_contact_phone"],
                "destination_address" => $params["destination_address"],
                "destination_postal_code" => $params["destination_postal_code"],

                "reference_id" => $params["reference_id"],
                "courier_insurance" => $params["courier_insurance"],

                "courier_company" => $params["courier_company"],
                "courier_type" => $params["courier_type"],
                "delivery_type" => "now",
                "items" => $params["items"]

            ]), 'application/json')
            ->post($url."/v1/orders");

        if ($res->status() == 200)
            return $res->json();
        else{
            Log::error("Biteship order error",  $res->json());
            throw new Exception($res->json()['error']);
        }

        return null;
    }

    public function byLatLong($params){

        $url = env("BITESHIP_URL");
        $key = env("BITESHIP_KEY");
        $res = Http::withHeaders([
                "authorization" => $key
            ])
            ->withBody(json_encode([
                "origin_contact_name" => $params["origin_contact_name"],
                "origin_contact_phone" => $params["origin_contact_phone"],
                "origin_address" =>  $params["origin_address"],
                "origin_coordinate" => [
                    "latitude" => $params["origin_latitude"],
                    "longitude" => $params["origin_longitude"],
                ],

                "destination_contact_name" => $params["destination_contact_name"],
                "destination_contact_phone" => $params["destination_contact_phone"],
                "destination_address" => $params["destination_address"],
                "destination_coordinate" => [
                    "latitude" => $params["destination_latitude"],
                    "longitude" => $params["destination_longitude"],
                ],

                "courier_company" => $params["courier_company"],
                "courier_type" => $params["courier_type"],
                "delivery_type" => "now",
                "items" => $params["items"]

            ]), 'application/json')
            ->post($url."/v1/orders");

            if ($res->status() == 200)
                return $res->json();
            else{
                Log::error("Biteship order error",  [$res->json()]);
                throw new Exception($res->json()['error']);
            }

            return null;

    }


    public function confirm($id){

        $url = env("BITESHIP_URL");
        $key = env("BITESHIP_KEY");
        $res = Http::withHeaders([
                "authorization" => $key
            ])
            ->post($url."/v1/orders/".$id."/confirm");

        if ($res->status() == 200)
            return $res->json();
        else
            Log::error("Biteship order error",  [$res->json()]);

        return null;
    }

}
