<?php


class NotificationHelper
{
    const ACTION_DISKUSI = 1;
    const ACTION_CERITA = 2;
    const ACTION_REPORT = 3;
    const ACTION_REPORTED = 4;
    const ACTION_RECOMMENDATION = 5;

    const ACTION_PANCEK = 6;

    const CATEGORY_DISCUSSION = 0;
    const CATEGORY_MENTION_REPLY = 1;
    const CATEGORY_REPORT = 2;
    const CATEGORY_RECOMMENDATION = 3;
    
    const CATEGORY_SPI = 51;

    const CATEGORY_PANCEK_ASSIGN = 61;
    const CATEGORY_PANCEK_ASSIGN_KPPBC = 62;
    const CATEGORY_PANCEK_VERIFY = 63;
    const CATEGORY_PANCEK_REKAPITULASI = 64;
    const CATEGORY_PANCEK_FORM = 65;

    public static function getPayloadData($action, $actionId)
    {
        if ($action == self::ACTION_DISKUSI) {
            return [
                "name" => "diskusi-detail",
                "query" => [
                    "uuid" => $actionId
                ]
            ];
        } else if ($action == self::ACTION_CERITA) {
            return [
                "name" => "cerita-detail",
                "query" => [
                    "uuid" => $actionId
                ]
            ];
        } else if ($action == self::ACTION_REPORT) {
            return [
                "name" => "admin-reports"
            ];
        } else if ($action == self::ACTION_RECOMMENDATION) {
            return [
                "name" => "rekomendasi-detail",
                "query" => [
                    "user_id" => $actionId
                ]
            ];
        }
        else {
            return [
                "name" => "home"
            ];
        }
    }


    public static function send($userId, $senderUserId, $body, $action, $actionId, $clickAction, $category, $bodyEn)
    {
        $notif = new Notifications();
        $notif->owner_user_id = $userId;
        $notif->sender_user_id = $senderUserId;
        $notif->message = $body;
        $notif->action = $action;
        $notif->action_id = $actionId;
        $notif->category = $category;
        $notif->message_en = $bodyEn;
        $notif->created_at = date('Y-m-d H:i:s');
        $notif->create();

        try {
            $senderName = UmUser::findFirst(
                [
                    "columns" => "nama",
                    "um_id = :um_id:",
                    "bind" => [
                        "um_id" => $senderUserId
                    ]
                ]
            )->nama;
        } catch (\Exception $e) {
            $senderName = "[Unknown]";
        }

        $result = FcmDeviceToken::find(
            [
                "columns" => "distinct device_token",
                "user_id = :user_id:",
                "bind" => [
                    "user_id" => $userId
                ]
            ]
        )
            ->toArray();

        try {
            $useBackground = AppConfig::getValueByKode("USE_NOTIF_BACKGROUND") === 'true';
        } catch (\Exception $e) {
            $useBackground = false;
        }

        foreach ($result as $res) {
            $message = $senderName . " " . $body;
            if ($useBackground) {
                FcmHelper::logNotif($userId, $res["device_token"], "Jaga", $message, [
                    "success" => true,
                    "path" => $clickAction,
                    "notification_foreground" => true,
                    "payloadData" => self::getPayloadData($action, $actionId)
                ]);
            } else {
                FcmHelper::sendNotifBg($res["device_token"], "Jaga", $message, [
                    "success" => true,
                    "path" => $clickAction,
                    "notification_foreground" => true,
                    "payloadData" => self::getPayloadData($action, $actionId)
                ], $clickAction);
            }
        }
    }

    public static function sendWithoutSender($userId, $body, $action, $actionId, $clickAction, $category, $bodyEn)
    {
        $notif = new Notifications();
        $notif->owner_user_id = $userId;
        $notif->sender_user_id = $userId;
        $notif->message = $body;
        $notif->action = $action;
        $notif->action_id = $actionId;
        $notif->category = $category;
        $notif->message_en = $bodyEn;
        $notif->create();

        $result = FcmDeviceToken::find(
            [
                "columns" => "distinct device_token",
                "user_id = :user_id:",
                "bind" => [
                    "user_id" => $userId
                ]
            ]
        )
            ->toArray();

        try {
            $useBackground = AppConfig::getValueByKode("USE_NOTIF_BACKGROUND") === 'true';
        } catch (\Exception $e) {
            $useBackground = false;
        }

        foreach ($result as $res) {
            $message = $body;
            if ($useBackground) {
                FcmHelper::logNotif($userId, $res["device_token"], "Jaga", $message, [
                    "success" => true,
                    "path" => $clickAction,
                    "notification_foreground" => true,
                    "payloadData" => self::getPayloadData($action, $actionId)
                ]);
            } else {
                FcmHelper::sendNotifBg($res["device_token"], "Jaga", $message, [
                    "success" => true,
                    "path" => $clickAction,
                    "notification_foreground" => true,
                    "payloadData" => self::getPayloadData($action, $actionId)
                ], $clickAction);
            }
        }
    }

    public static function sendDirect($userId, $senderUserId, $body, $action, $actionId, $clickAction, $category, $bodyEn)
    {
        $notif = new Notifications();
        $notif->owner_user_id = $userId;
        $notif->sender_user_id = $senderUserId;
        $notif->message = $body;
        $notif->action = $action;
        $notif->action_id = $actionId;
        $notif->category = $category;
        $notif->message_en = $bodyEn;
        $notif->created_at = date('Y-m-d H:i:s');
        $notif->create();

        try {
            $senderName = UmUser::findFirst(
                [
                    "columns" => "nama",
                    "um_id = :um_id:",
                    "bind" => [
                        "um_id" => $senderUserId
                    ]
                ]
            )->nama;
        } catch (\Exception $e) {
            $senderName = "[Unknown]";
        }

        $result = FcmDeviceToken::find(
            [
                "columns" => "distinct device_token",
                "user_id = :user_id:",
                "bind" => [
                    "user_id" => $userId
                ]
            ]
        )
            ->toArray();

        foreach ($result as $res) {
            $message = $senderName . " " . $body;
            FcmHelper::sendNotifBg($res["device_token"], "Jaga", $message, [
                "success" => true,
                "path" => $clickAction,
                "notification_foreground" => true,
                "payloadData" => self::getPayloadData($action, $actionId)
            ], $clickAction);
        }
    }

    public static function batchSend($receiverIds, $senderUserId, $body, $action, $actionId, $clickAction, $category, $bodyEn)
    {
        foreach ($receiverIds as $key => $receiverId) {
            self::send(
                $receiverId,
                $senderUserId,
                $body,
                $action,
                $actionId,
                $clickAction,
                $category,
                $bodyEn
            );
        }
    }
}
