<?php

class PushApi extends Api
{
    public $apiName = 'push';

    public function __construct($requestUri, $encryption=true) {
        $this->requestWhitelist['createAction'] = 1;
        parent::__construct($requestUri, $encryption);
    }
    
    public function indexAction()
    {
        return $this->response(
            array(
                'error' => 'Data not found'
            ), 404);
    }

    /**
     * Method GET
     * Get single record (by id)
     * http://DOMAIN/address/1
     * @return string
     */
    public function viewAction()
    {
        //id must be the first parameter after /address/x
        $id = array_shift($this->requestUri);

        if($id && (int)$id>0){
            /*$db = new Db();
            $address = Address::getAddressById($db->getConnect(), (int)$id);
            if(is_array($address) && count($address)>0){
                return $this->response($address, 200);
            }*/
        }
        return $this->response(
            array(
                'error'=> 'Data not found'
            ), 404);
    }

    public function createAction()
    {
        error_log("PushApi::createAction()");
        $message = "Data not found";
        $player_id = $this->requestParams["player_id"] ?? "";
        $member_id = (int)($this->requestParams["member_id"] ?? "0");
        $cordova = $this->requestParams["cordova"] ?? "";
        $model = $this->requestParams["model"] ?? "";
        $platform = $this->requestParams["platform"] ?? "";
        $uuid = $this->requestParams["uuid"] ?? "";
        $version = $this->requestParams["version"] ?? "";
        $manufacturer = $this->requestParams["manufacturer"] ?? "";
        $is_virtual = $this->requestParams["is_virtual"] ?? "false";
        $country = $this->requestParams["country"] ?? "SG";
        $sessionid = $this->requestParams["sessionid"] ?? "";
        $access_token = $this->requestParams["access_token"] ?? "";
        $refresh_token = $this->requestParams["refresh_token"] ?? "";
        error_log("member_id=".$member_id);
        error_log("player_id=".$player_id);
        error_log("sessionid=".$sessionid);
        error_log("access_token=".$access_token);
        error_log("refresh_token=".$refresh_token);
        try {
            $db = new Db();
            $session = Auth::getMemberSession($db->getConnect(), $member_id, $sessionid);
            if ($session==NULL || !isset($session["id"])) {
                error_log('Invalid member session ID - cannot save player ID!');
                throw new Exception('Invalid member session ID');
            }
            if ($member_id<1 || $player_id=="") {
                throw new Exception('Invalid member and/or player ID');
            }
            $data = [
                'member_id' => $member_id,
                'player_id' => $player_id,
                'cordova' => $cordova,
                'model' => $model,
                'platform' => $platform,
                'uuid' => $uuid,
                'version' => $version,
                'manufacturer' => $manufacturer,
                'is_virtual' => $is_virtual,
                'country' => $country,
                'access_token' =>  $access_token,
                'refresh_token' =>  $refresh_token 
            ];
            $result = Push::getMemberPlayerId($db->getConnect(), $member_id, $player_id, $access_token);
            if ($result!=NULL && is_array($result) && $result["id"]>0) {
                if ($result["access_token"]=="" || $result["refresh_token"]=="") {
                    $result = AuthApi::generateTokens($db, $result["id"]);
                    $data['access_token'] = $result['access_token'];
                    $data['refresh_token'] = $result['refresh_token'];
                }
                // This will update the data...
                $updated = Push::saveMemberPlayerId($db->getConnect(), $data);
                if ($updated!=NULL && is_array($updated) && $updated["id"]>0) {
                    return $this->response($updated, 200);
                }
                return $this->response($result, 200);
            }
            $result = Push::saveMemberPlayerId($db->getConnect(), $data);
            if ($result!=NULL && is_array($result) && $result["id"]>0) {
                if ($result["access_token"]=="" || $result["refresh_token"]=="") {
                    $result = AuthApi::generateTokens($db, $result["id"]);
                }
                return $this->response($result, 200);
            }
            return $this->response(array("error" => "Failed to save"),500);
        } catch (Exception $e) {
            $message = $e->getMessage();
        } 
        return $this->response(
            array(
                "error" => $message
            ), 404);
    }

    public function updateAction()
    {
        return $this->response(
            array(
                "error" => "Update error"
            ), 400);
    }

    public function deleteAction()
    {
        return $this->response(
            array(
                "error" => "Delete error"
            ), 500);
    }
}
