<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Booking;
use App\User;
use App\Installment;
use App\Sms;

class SmsController extends Controller
{
   public function pushSms(Request $request)
   { 
        // RETURN 0 => MEAN SMS FAIL DUE TO ANY CASE
        // RETURN 1 => MEAN SMS SEND AND JAR FILE IS EXECUTED AND SMS STATUS UPDATE
        // RETURN 2 => MEAN SMS NUMBER FORMAT IS WRONG [0300-1234567]

    try 
    {

       $booking = new Booking;
        //{
            // CRYPT PART
       $id   = decrypt($request->id);

       $type = decrypt($request->type);
            // END CRYPT PART

            // DEFAULT VARABLE PART
       $object['contact']         = 0;

       $object['message']         = "NA";

       $object['booking_id']      = 0;

       $object['installment_id']  = 0;

       $object['type']            = $type;
            // END DEFAULT VARIABLE PART


            // THIS CONDITION WORK ONLY WHEN DUE AMOUNT IS BOOKING WISE (COLLECTION OF INSTALLMENT)
       if($type =='Booking' || $type =='Installment')
       {


        if($type == 'Booking')
        {

            $booking = $booking->getBookingById($id);

            if(isset($booking->id))
            {
                $installment = new Installment;

                $object['installment_id'] = 0;    
                $object['amount']    = $installment->getOverDueByBooking($booking->id)->balance;

            }
        }

            // THIS CONDITION IS WORK WHEN ONLY SINGLE INSTALLMENT 
        else if($type == "Installment")
        {
            $installment = Installment::find($id);



            if(isset($installment->booking_id))
            {
                $object['installment_id'] = $installment->id;

                $booking = $booking->getBookingById($installment->booking_id);

                $object['amount']    = ($installment->amount) - ($installment->received_amount);


            }
        }
        $object['booking_id']     = $booking->id;

        $object['contact']        = $booking->user->contact;

        $object['user_id']        = $booking->user_id;

        $name                     = $booking->user->name;


        $plot_detail = $booking->plot->size." ".$booking->plot->category;
        $object['message']  = "Dear $name! According to the payment plan you opted, your payment against your plot is due. Please regard this matter as urgent and make the payment immediately. If you have already sent the payment, please disregard this notice.";

                    //BEFORE SMS SEND IS CHECK NUMBER FORMAT IS OK?
        if($this->checkContactFormat($object['contact']))
        {
            $other_format = '/^([0-9]{4})[-]([0-9]{7})$/';
                        // THIS CONDITION ONLY FOR GIVEN FORMAT 0303-4423909 THIS IS CORRECT FORMAT
            if(!preg_match($other_format,$object['contact']))
            {
                $object['contact'] = $this->correctFormat($object['contact']);
            }

        }
        else
        {
            return 2;
        }

                    // SEND SMS VIA SMS TABLE
        $sms = new Sms;

        return $sms->storeSms($object);
                    // END SEND SMS VIA SMS TABLE

        
    }       
    else if($type == "Birthday")
    {

        $user = new User;

        $curr_user = $user->getUserById($id);

        if(isset($curr_user->id))
        {
            $object['installment_id'] = 0;

            $object['booking_id']  = 0;

            $object['user_id']  = $curr_user->id;

            $object['contact']     = $curr_user->contact;

                    //BEFORE SMS SEND IS CHECK NUMBER FORMAT IS OK?
            if($this->checkContactFormat($object['contact']))
            {
                $other_format = '/^([0-9]{4})[-]([0-9]{7})$/';
                        // THIS CONDITION ONLY FOR GIVEN FORMAT 0303-4423909 THIS IS CORRECT FORMAT
                if(!preg_match($other_format,$object['contact']))
                {
                    $object['contact'] = $this->correctFormat($object['contact']);

                    $object['message'] =  $object['contact'];
                }

            }
            else
            {
                return 2;
            }

                    // SEND SMS VIA SMS TABLE
            $sms = new Sms;

            return $sms->storeSms($object);
                    // END SEND SMS VIA SMS TABLE


        }
    }       
}
catch (DecryptException $e) 
{

}
return 0;
}

public function getSmsByStatus()
{
    $sms = new Sms;
    return $sms->getSmsByStatus();
}

public function updateSmsStatus($id)
{
    $sms = new Sms;
    $sms->updateSmsStatus($id);
}

public function checkContactFormat($phone)
{
        // FORMAT IS 03001234567


    $format = '/^([0-9]{4})([0-9]{7})$/';

    $other_format = '/^([0-9]{4})[-]([0-9]{7})$/';

    if(preg_match($format,$phone))
    {
        return true;
    }
    else if(preg_match($other_format,$phone))
    {
        return true;
    }

    return false;

}


public function correctFormat($phone)
{
    $new_format = "";

    for($i = 0; $i<strlen($phone); $i++)
    {

        if($i == 4)
        {
            $new_format.='-';
        }

        $new_format.=$phone[$i];
    }

    return $new_format;
}
}
