<?php

namespace auctionforge\inc\model;

use auctionforge\inc\abs\Model;
use auctionforge\inc\Debug;
use auctionforge\inc\InstanceDB;

class FirebaseUser extends Model
{
    const TABLE_APP_USERS = "bp_firebase_users";

    private $created;
    private $firebaseId;
    private $bidspiritUserEmail;

    /**
     * @param $params array
     * @return void
     */
    public function loadAttributes($params) {
        $this->setBidspiritUserEmail($params['user']);
        $this->setCreated(time());
        parent::loadAttributes($params);
    }

    public function save()
    {
        if (!empty($this->getId())) {
            $this->updateModel();
        } else {
            $this->saveModel();
        }
    }

    private function updateModel() {
        $db = InstanceDB::getInstanceDB();
        $sql = "UPDATE " . $db->prefix . self::TABLE_APP_USERS . " SET `firebase_id` = '%s' WHERE `bidspirit_user_email` = %s";
        $sql = $db->prepare($sql, $this->getFirebaseId(), $this->getBidspiritUserEmail());
        return $db->query($sql);
    }

    private function saveModel() {
        $db = InstanceDB::getInstanceDB();
        $sql = "INSERT INTO " . $db->prefix . self::TABLE_APP_USERS . " (`firebase_id`, `bidspirit_user_email`) VALUES ('%s', '%s')";
        $sql = $db->prepare($sql, $this->getFirebaseId(), $this->getBidspiritUserEmail());
        return $db->query($sql);
    }

    public function load()
    {
        if (!empty($this->getBidspiritUserEmail())) {
            $db = InstanceDB::getInstanceDB();
            $sql = "SELECT * FROM " . $db->prefix . self::TABLE_APP_USERS . " WHERE `bidspirit_user_email` = '%s'";
            $sql = $db->prepare($sql, $this->getBidspiritUserEmail());
            $result = $db->get_row($sql);

            if (!empty($result) && isset($result->id)) {
                $this->setId($result->id);
                $this->setName($result->bidspirit_user_email);
                if (empty($this->getFirebaseId())) {
                    $this->setFirebaseId($result->firebase_id);
                }
            }

        }
    }

    public static function deviceTokensForUsers($usersList)
    {
        if (!empty($usersList)) {
            $db = InstanceDB::getInstanceDB();
            $sql = "SELECT `firebase_id` FROM `" . $db->prefix . "bp_firebase_users` WHERE bidspirit_user_email IN ('" . implode('\',\'', $usersList) . "')";
            return $db->get_col($sql);
        }
    }

    public static function deviceTokens()
    {
        $db = InstanceDB::getInstanceDB();
        $sql = "SELECT `firebase_id` FROM `" . $db->prefix . "bp_firebase_users` WHERE `bidspirit_user_email` IS NOT NULL AND `firebase_id` IS NOT NULL";
        return $db->get_col($sql);
    }

    /**
     * @return mixed
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * @param  mixed  $created
     */
    public function setCreated($created)
    {
        $this->created = $created;
    }

    /**
     * @return mixed
     */
    public function getFirebaseId()
    {
        return $this->firebaseId;
    }

    /**
     * @param  mixed  $firebaseId
     */
    public function setFirebaseId($firebaseId)
    {
        $this->firebaseId = urldecode($firebaseId);
    }

    /**
     * @return mixed
     */
    public function getBidspiritUserEmail()
    {
        return $this->bidspiritUserEmail;
    }

    /**
     * @param  mixed  $bidspiritUserEmail
     */
    public function setBidspiritUserEmail($bidspiritUserEmail)
    {
        $this->bidspiritUserEmail = $bidspiritUserEmail;
    }
}
