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


class TB_Server_Core_PushNotification {

    // Singleton pattern
    static protected $instance = NULL;
    protected function __construct() {}
    protected function __clone() {}

    // Members
    /** @var $gcm Francis_Utils_PushGoogle  */
    private $gcm = NULL;

    /** @var $apn Francis_Utils_PushIOS  */
    private $apn = NULL;

    /**
     * Instance
     * @return null|TB_Server_Core_PushNotification
     */
    static public function get() {
        if ( is_null( self::$instance ) ) {
            self::$instance = new self();
            $apiKey = Francis_Utils_Config::get( 'push.gcm.apikey' );
            self::$instance->gcm = Francis_Utils_PushGoogle::get( array(
                'api_key' => Francis_Utils_Config::get( 'push.gcm.apikey' )
            ));
            try {
                self::$instance->apn = new Francis_Utils_PushIOS( Francis_Utils_PushIOS::ENV_PRODUCTION );
            } catch ( Exception $e ) {
                TB_Server_Utils_Log::get()->log( $e );
            }
        }
        return self::$instance;
    }

    /**
     * Sends given message
     * @param array $message
     */
    public function sendTeam( array $message ) {
        $onliner = TB_Server_Core_Session::get()->getProfile();

        $dbh = TB_Shared_Ent_TeamData_Profile::getDbh();
        $sql = "SELECT account_id FROM " . TB_Shared_Ent_TeamData_Profile::getTable() . " WHERE team_id = :team_id ";
        $stmt = $dbh->prepare( $sql );
        $stmt->execute( array(
            ':team_id' => $onliner->team_id
        ));
        $teamAccIds = $stmt->fetchAll( PDO::FETCH_COLUMN, 0 );

        $this->pushGoogle( $message, $teamAccIds );
        $this->pushIOS( $message, $teamAccIds );
    }

    /**
     * @param array $message
     * @param array $teamAccIds
     */
    private function pushIOS( array $message, array $teamAccIds ) {
        // Nothing to send means nothing to do
        if ( !is_array( $teamAccIds ) || 0 == count( $teamAccIds ) || is_null( $this->apn ) ) return;

        try {
            // Collect reg_ids
            $dbh = TB_Shared_Ent_Core_DeviceReg::getDbh();
            $inQuery = implode(',', array_fill(0, count($teamAccIds), '?'));
            $sql  = "SELECT reg_id FROM " . TB_Shared_Ent_Core_DeviceReg::getTable() . " ";
            $sql .= "WHERE platform = 'IOS' AND account_id IN( " . $inQuery . " )";

            $stmt = $dbh->prepare( $sql );
            foreach( $teamAccIds as $k => $id ) {
                $stmt->bindValue(($k+1), $id);
            }
            $stmt->execute();

            $regIds = $stmt->fetchAll( PDO::FETCH_COLUMN, 0 );

            // Push
            $this->apn->send( $regIds, $message );

        } catch ( Exception $e ) {
            TB_Server_Utils_Log::get()->log( $e );
        }
    }

    /**
     * @param array $message
     */
    private function pushGoogle( array $message, array $teamAccIds ) {

        // Nothing to send means nothing to do
        if ( !is_array( $teamAccIds ) || 0 == count( $teamAccIds ) ) return;

        try {
            // Collect reg_ids
            $dbh = TB_Shared_Ent_Core_DeviceReg::getDbh();
            $inQuery = implode(',', array_fill(0, count($teamAccIds), '?'));
            $sql  = "SELECT reg_id FROM " . TB_Shared_Ent_Core_DeviceReg::getTable() . " ";
            $sql .= "WHERE platform = 'ANDROID' AND account_id IN( " . $inQuery . " )";

            $stmt = $dbh->prepare( $sql );
            foreach( $teamAccIds as $k => $id ) {
                $stmt->bindValue(($k+1), $id);
            }
            $stmt->execute();

            $regIds = $stmt->fetchAll( PDO::FETCH_COLUMN, 0 );

            // Push
            $this->gcm->send( $regIds, $message );

        } catch ( Exception $e ) {
            TB_Server_Utils_Log::get()->log( $e );
        }
    }
}