<?php
/**
 * Created by PhpStorm.
 * User: varun palekar
 * Github: varunpalekar
 * Date: 28-01-2016
 * Time: 18:21
 */

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use DB ;



class OTPverification extends Controller
{
    private $OTP_string = '0123456789' ;
    public function verifyOTP(Request $request)
    {
        if($request->has('otp')
        && $request->has('id')
        )
        {
            $result = DB::table('otp_verification')
                ->select('id', 'info' )
                ->where('otp' , '=' , $request->input('otp'))
                ->where('id' , '=' , $request->input('id'))
                ->first();

            $info = json_decode($result['info']) ;

            if($result)
            {
                DB::table('otp_verification')
                    ->where('id' , '=' , $result['id'] )
                    ->delete();

                if($request->input('type') == 'user')
                {
                    /**
                     * Verify user
                     */
                    $this->verifyUser($info->user) ;

                    return new JsonResponse(array(
                        "status#" => "1",
                        "data" => "OTP verified Successfully"
                    )) ;
                }
                elseif($request->input('type') == 'plantbee')
                {
                    /**
                     * Plantbee entry for OTP
                     */
                    $this->verifyPlantbeeLead($info->lead) ;

                    if(!empty($info->user))
                        $this->verifyUser($info->user) ;

                    return new JsonResponse(array(
                        "status#" => "1",
                        "data" => "OTP verified Successfully"
                    )) ;
                }
            }
            else
                return new JsonResponse(array(
                    "status#" => "1",
                    "message#" => "Wrong OTP"
                )) ;

        }
    }

    /**
     * Used to update verify user by userid
     * @param $user_id: memid
     */
    private function verifyUser($user_id)
    {
        DB::table($_ENV['USER_TABLE'])
            ->where(  'id' , '=',$user_id  )
            ->update( [ $_ENV['VERIFIED_COLUMN'] => 1] ) ;
    }

    /**
     * Used to set otp verify for leads in plantbee according to lead id
     * @param $lead_id: lead id in plantbee
     */
    private function verifyPlantbeeLead($lead_id)
    {
        DB::table('leads')
            ->where( 'id', '=' ,$lead_id  )
            ->update( [ 'otp_verify' => true ] ) ;
    }

    public function sendOTP( $type , $mobile, $info )
    {
        $insert = array() ;
        $msg = null ;
        if($type == 'user')
        {
            $msg = "Welcome to Steelmint\nPlease verify Mobile number\nOTP: ";
        }
        elseif($type == 'plantbee')
        {
            $msg = "Welcome to Plantbee\nPlease verify Mobile number\nOTP: ";
        }
        elseif ($type == 'user-plantbee')
        {
            $msg = "Welcome to Plantbee\nPlease verify Mobile number\nOTP: ";
        }

        $string_shuffled = str_shuffle($this->OTP_string);
        $insert['otp'] = substr($string_shuffled, 1, env('OTP_LENGTH' , 6 ));
        $insert['mobile'] = $mobile ;
        $insert['type'] = $type ;
        $insert['info'] = json_encode($info) ;

        $msg .= $insert['otp'] ;

        $link = $_ENV['SMS_GATEWAY']."&to=".$mobile."&sender=".env('SMS_GATEWAY_SENDER' , 'MINTTT')."&message=".urlencode ($msg) ;

        $request = Request::create($link , 'GET' ) ;
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => $link
        ));

        if (! curl_exec($curl))
            abort(500, 'OTP sending problem:'.curl_error($curl));

        $ins_id =  DB::table('otp_verification')
            ->insertGetID($insert) ;

        return $ins_id ;

    }
}