<?php

require 'db.php';


$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

function sendNotificationMessage($title,$message,$image,$mode) {
    $content      = array(
        "en" => $message
    );
    if($image != "")
    {

        if($mode == "edit")
        {
            $split = explode("/Arabian_hut/",$image);
            if($split[0] != null)
            $image = $split[1];
            else $image = "img/logo.jpg";
        }

        $fields = array(
        'app_id' => "6b5ae8dd-3b56-4960-9922-f78b61e59499",
        'included_segments' => array(
            'Subscribed Users'
        ),
        'contents' => $content,
        'headings' => array("en"=>$title),
        'big_picture' => 'https://api.iconxtechnologies.in/Arabian_hut/'.$image);
    }
    else
    {
        $fields = array(
        'app_id' => "6b5ae8dd-3b56-4960-9922-f78b61e59499",
        'included_segments' => array(
            'Subscribed Users'
        ),
        'contents' => $content,
        'headings' => array("en"=>$title));
    }
    
    $fields = json_encode($fields);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Authorization: Basic ZjY1YmM0NjAtMzFmNi00ZTBjLWIwNDYtNTg3MDRhZjMxN2Fl'
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    return $response;
}

if(isset($postdata) && !empty($postdata))
{
    $mode = mysqli_real_escape_string($con, trim($request->mode));
    $noti_id = mysqli_real_escape_string($con, trim($request->notification_id));
    $title = mysqli_real_escape_string($con, trim($request->form_value->title));
    $message = mysqli_real_escape_string($con, trim($request->form_value->message));
    $filesource = mysqli_real_escape_string($con, trim($request->form_value->fileSource));
    $image = mysqli_real_escape_string($con, trim($request->form_value->image));
    
    if($mode == "add")
    {
        if($title == null ||  $title == "" || $message == null ||  $message == "")
        {
            $rows = array('status'=>false,'message' => 'Fields Missing');
            echo json_encode($rows);
        }
        else
        {
            if($filesource == null || $filesource == "")
            {
                $sql = "INSERT INTO notification(title,message,send_count) VALUES ('$title','$message',0)";
                $result = mysqli_query($con,$sql);
                if($result)
                {
                    $response = sendNotificationMessage($title,$message,"",$mode);
                    $data = json_decode($response, true);
                    if($data['id'] != null)
                    {
                        $rows = array('status'=>true,'message' => 'Notification Send Success');
                        echo json_encode($rows);
                    }
                    else
                    {
                        $rows = array('status'=>false,'message' => 'Notification Send Failure');
                        echo json_encode($rows);
                    }
                }
            }
            else
            {
                $folderPath = "noti/";
                $image_parts = explode(";base64,", $filesource);
                $image_type_aux = explode("image/", $image_parts[0]);
                $image_type = $image_type_aux[1];
                $image_base64 = base64_decode($image_parts[1]);
                $file = $folderPath . uniqid() . '.png';
                file_put_contents($file, $image_base64);
                
                $sql = "INSERT INTO notification(title,image,message,send_count) VALUES ('$title','$file','$message',0)";
                $result = mysqli_query($con,$sql);
                if($result)
                {
                    $response = sendNotificationMessage($title,$message,$file,$mode);
                    $data = json_decode($response, true);
                    if($data['id'] != null)
                    {
                        $rows = array('status'=>true,'message' => 'Notification Send Success');
                        echo json_encode($rows);
                    }
                    else
                    {
                        $rows = array('status'=>false,'message' => 'Notification Send Failure');
                        echo json_encode($rows);
                    }
                }
            }
            
        }
    }
    
    else if($mode == "edit")
    {
        if($title == null ||  $title == "" || $message == null ||  $message == "" || $noti_id == null || $noti_id == "")
        {
            $rows = array('status'=>false,'message' => 'Fields Missing');
            echo json_encode($rows);
        }
        else
        {
            $sel = $con->query("select * from notification where id = '$noti_id'");
            if($rows = $sel->fetch_assoc())
            {
                $sendcount = $rows['send_count'] + 1;
                $sql = "UPDATE notification set send_count = '$sendcount' where id = '$noti_id'";
                $result = mysqli_query($con,$sql);
                if($result)
                {
                    $response = sendNotificationMessage($title,$message,$image,$mode);
                    $data = json_decode($response, true);
                    if($data['id'] != null)
                    {
                        $rows = array('status'=>true,'message' => 'Notification Re-send Success');
                        echo json_encode($rows);
                    }
                    else
                    {
                        $rows = array('status'=>false,'message' => 'Notification Re-send Failure');
                        echo json_encode($rows);
                    }

                }
                else
                {
                    $rows = array('status'=>false,'message' => $sendcount);
                    echo json_encode($rows);
                }
            }
            else
            {
                $rows = array('status'=>false,'message' => 'Notification Re-send Failure2');
                echo json_encode($rows);
            }
            
        }
    }
    
    else if($mode == "delete")
    {
        if($noti_id == null || $noti_id == "")
        {
            $rows = array('status'=>false,'message' => 'Notification Error');
            echo json_encode($rows);
        }
        else
        {
            $sql = "DELETE FROM notification where id ='$noti_id'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Notification Deleted');
                echo json_encode($rows);
            }
        }
    }

}

?>
