<?php

namespace FOP;

class Push {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function getPublicKey() { 
    
    
    // --- BLOCK: main ---
    return \FOP\Configuration::getFOPPushPublicKey();
    
  }

  public static function getPrivateKey() { 
    
    
    // --- BLOCK: main ---
    return \FOP\Configuration::getFOPPushPrivateKey();
    
  }

  public static function getSubscriptions($userid) { 
    
    
    // --- BLOCK: main ---
    
        if ($userid) {
             if (is_array($userid)) {
                $idlist = new \FOP\IDList;
                foreach ($userid AS $id) $idlist->push($id);
                $userid = $idlist;
             } else if (is_string($userid)) {
                $idlist = new \FOP\IDList;
                $idlist->push($userid);
                $userid = $idlist;
             }
             $rows = \FOP\Database::query('SELECT endpoint, publicKey, authToken, contentEncoding FROM fop_pushsubscriptions WHERE ' . $idlist->toSQL('userid'))->fetchAll(\PDO::FETCH_ASSOC);
        } else {
            $rows = \FOP\Database::query('SELECT endpoint, publicKey, authToken, contentEncoding FROM fop_pushsubscriptions')->fetchAll();    
        }
        $result = array();
        foreach($rows as $row){
            $sub = \Minishlink\WebPush\Subscription::create($row);
            array_push($result, $sub);
        }
        return $result;
    
    
  }

  public static function getSubscriptionsByKey($key) { 
    
    
    // --- BLOCK: main ---
    
        if ($key) {
             if (is_array($key)) {
                $idlist = new \FOP\IDList;
                foreach ($key AS $id) $idlist->push($id);
                $key = $idlist;
             } else if (is_string($key)) {
                $idlist = new \FOP\IDList;
                $idlist->push($key);
                $key = $idlist;
             }
             $rows = \FOP\Database::query('SELECT endpoint, publicKey, authToken, contentEncoding FROM fop_pushsubscriptions WHERE ' . $idlist->toSQL('publicKey'))->fetchAll(\PDO::FETCH_ASSOC);
        } else {
            $rows = \FOP\Database::query('SELECT endpoint, publicKey, authToken, contentEncoding FROM fop_pushsubscriptions')->fetchAll();    
        }
        $result = array();
        foreach($rows as $row){
            $sub = \Minishlink\WebPush\Subscription::create($row);
            array_push($result, $sub);
        }
        return $result;
    
    
  }

  public static function sendMessage($subscriptions, $payload, $ttl = 604800 ) { 
    
    
    // --- BLOCK: main ---
    
            $auth = array(
                'VAPID' => array('subject' => \FOP\Configuration::getBaseurl(),
                'publicKey' => \FOP\Configuration::getFOPPushPublicKey(), 'privateKey' => \FOP\Configuration::getFOPPushPrivateKey())
            );

            $webPush = new \Minishlink\WebPush\WebPush($auth, ['TTL' => $ttl]);
            
            foreach($subscriptions as $subscription){
                $webPush->sendOneNotification($subscription, $payload);
            }

            foreach($webPush->flush() as $report){
                $endpoint = $report->getRequest()->getUri()->__toString();
                if(!$report->isSuccess()) {
                    \FOP\Database::query('DELETE FROM fop_pushsubscriptions WHERE endpoint=' . \FOP\Database::quote($endpoint));
                }
            }
    
    
  }

  public static function registerSubscriptionAction($endpoint, $publicKey, $authToken, $contentEncoding) { 
    
    
    // --- BLOCK: access ---
    
    // --- BLOCK: main ---
    
        

        $r = \FOP\Database::query('INSERT INTO fop_pushsubscriptions(endpoint, publicKey, authToken, contentEncoding, userid) VALUES(' . \FOP\Database::quote($endpoint)
            . ',' . \FOP\Database::quote($publicKey) . ',' . \FOP\Database::quote($authToken) . ',' . \FOP\Database::quote($contentEncoding) .
            ', 0) ON DUPLICATE KEY UPDATE publicKey=' . \FOP\Database::quote($publicKey) . ', authToken=' . \FOP\Database::quote($authToken) . ', contentEncoding=' . \FOP\Database::quote($contentEncoding));
        
        
            if(!\FOP\User::getIsGuest()){
                \FOP\Database::query('UPDATE fop_pushsubscriptions SET userid=' . \FOP\Database::quote(\FOP\User::getID()) . ' WHERE endpoint=' . \FOP\Database::quote($endpoint));
            }
        

        if($r->rowCount() == 1){
            
        } else {
            return array('Status' => 'Duplicate');
		  }

        return array('Status' => 'OK');
    
    
  }


  
  

  
  
}



