<?php
/**
 * Created by PhpStorm.
 * User: sunil
 * Date: 28/11/18
 * Time: 12:39 PM
 */

namespace App;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class GooglePlaces
{
    private $request;
    private $endPointDevelopment = 'https://maps.googleapis.com/maps/api/geocode/json';
    private $api_key = "AIzaSyBJ5sOPcuh4edXH5KgE3vKqeZCfyO9JlHY";
    private $pincode;
    private $response;

//    public function __construct(Request $request){
//        $this->request = $request;
//        $this->setup();
//    }

    public static function getCityFromPincodeV1($pincode){
        $curlresponse = file_get_contents("http://postalpincode.in/api/pincode/".$pincode);
        $curlresponse = json_decode($curlresponse);
        if(isset($curlresponse->Status)){
            if($curlresponse->Status == "Success") {
                if(isset($curlresponse->PostOffice[0])){
                    $data['status']  = 'OK';
                    $data['message'] = 'Valid Pincode';
                    $data['state'] = $curlresponse->PostOffice[0]->State;
                    $data['district'] = $curlresponse->PostOffice[0]->District;
                    $data['region'] = $curlresponse->PostOffice[0]->Region;
                }
            }else{
                $data['status']  = 'failure';
                $data['message'] = $curlresponse->Message;
            }
        }else{
            $data['status']  = 'failure';
            $data['message'] = "Something went wrong";
        }
        return response()->json($data);
    }

    public static function getCityFromPincode($pincode){
        $url = "http://postalpincode.in/api/pincode/".$pincode;
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
                'cache-control: no-cache',
            ),
        ));
        $curlresponse = json_decode(curl_exec($curl));
        if(isset($curlresponse->Status)){
            if($curlresponse->Status == "Success") {
                if(isset($curlresponse->PostOffice[0])){
                    $data['status']  = 'OK';
                    $data['message'] = 'Valid Pincode';
                    $data['state'] = $curlresponse->PostOffice[0]->State;
                    $data['district'] = $curlresponse->PostOffice[0]->District;
                    $data['region'] = $curlresponse->PostOffice[0]->Region;
                }
            }else{
                $data['status']  = 'failure';
                $data['message'] = $curlresponse->Message;
            }
        }else{
            $data['status']  = 'failure';
            $data['message'] = "Something went wrong";
        }
        return response()->json($data);
    }

//    public function getCityFromPincode(){
//        $this->pincode = ($this->request->has('pincode')) ? $this->request->get('pincode') : false;
//        if($this->pincode) {
//            //$url = $this->endPointDevelopment . "?address=" . $this->pincode . "&region=in&key=" . $this->api_key;
//            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=400001&region=in&key=AIzaSyBJ5sOPcuh4edXH5KgE3vKqeZCfyO9JlHY";
//            $curl = curl_init();
//            curl_setopt_array($curl, array(
//                CURLOPT_URL => $url,
//                CURLOPT_RETURNTRANSFER => true,
//                CURLOPT_ENCODING => "",
//                CURLOPT_MAXREDIRS => 10,
//                CURLOPT_TIMEOUT => 30,
//                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//                CURLOPT_CUSTOMREQUEST => "GET",
//                CURLOPT_HTTPHEADER => array(
//                    'Content-Type: application/json',
//                    'cache-control: no-cache',
//                ),
//            ));
//            $response = curl_exec($curl);
//            print_r($response);
//        }
//    }
}
