<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/


class TB_Server_Control_Notification {
    /**
     * @param TB_Server_Core_RequestData $params
     * @return TB_Server_Core_Response
     */
    public static function get( TB_Server_Core_RequestData $params )
    {
        $resp = new TB_Server_Core_Response();

        $profile = TB_Server_Core_Session::get()->getProfile();
        $profileTs = TB_Shared_Ent_TeamData_ProfileTimestamp::getByProfileId( $profile->id );
        $notificationId = $params->get( 'notificationId' );

        // no specific id is set
        if ( is_null( $notificationId ) )
        {
            // Get all
            $notifications = TB_Shared_Ent_TeamData_Notification::getForProfileId( $profile->id );
            $resp->addData( 'notifications', $notifications );

            // Update timestamp
            $resp->addData( 'profile_timestamps', $profileTs );
        }
        else
        {
            $notification = TB_Shared_Ent_TeamData_Notification::get( $notificationId );
            if ( isset( $notification ) )
            {
                if ( $notification->receiver_profile_id != $profile->id )
                {
                    throw new \Exception( 'Not allowed to access.' );
                }

                // Flag as read
                $notification->is_read = 1;
                $notification->save();

                $resp->addData( 'notification', $notification );
            }
        }

        /*
        $profileTs->last_notification_update_dt = new DateTime();
        $profileTs->save();
        */

        return $resp;
    }

    /**
     * @param TB_Server_Core_RequestData $params
     * @return TB_Server_Core_Response
     */
    public static function markAllAsRead( TB_Server_Core_RequestData $params )
    {
        $resp = new TB_Server_Core_Response();

        TB_Shared_Ent_TeamData_Notification::markAllAsReadForProfileId(
            TB_Server_Core_Session::get()->getProfileId()
        );

        return $resp;
    }

//    /**
//     * @param TB_Server_Core_RequestData $params
//     * @return TB_Server_Core_Response
//     * @throws Exception
//     */
//    public static function create( TB_Server_Core_RequestData $params )
//    {
//        $resp = new TB_Server_Core_Response();
//
//        // Create
//        $profileIds = array();
//
//        $tps = TB_Server_Core_Session::get()->getTeamProfiles();
//        /** @var TB_Shared_Ent_TeamData_ProfileTeam $tp */
//        $tp = $tps[ 0 ];
//
//        $resp->addData( 'res', 'ok' );
//        return $resp;
//
//
//    }

//    /**
//     * @param TB_Server_Core_RequestData $params
//     * @return TB_Server_Core_Response
//     * @throws Exception
//     */
//    public static function delete( TB_Server_Core_RequestData $params )
//    {
//        $resp = new TB_Server_Core_Response();
//
//        /** @var TB_Shared_Ent_TeamData_Profile $profile */
//        $profile = TB_Server_Core_Session::get()->getProfile();
//
//        /** @var TB_Shared_Ent_TeamData_Notification $notification */
//        $notification = TB_Shared_Ent_TeamData_Notification::get( $params->get( 'notification_id' ) );
//
//        if ( isset( $notification ) && $notification->receiver_profile_id == $profile->id )
//        {
//            $notification->delete();
//        }
//        else
//        {
//            throw new Exception( 'No rights to delete notification' );
//        }
//
//        return $resp;
//    }
} 