<?php

require_once(BASE_DATA_PATH . '/api/notification/android/AndroidCustomizedcast.php');
require_once(BASE_DATA_PATH . '/api/notification/ios/IOSCustomizedcast.php');
require_once(BASE_DATA_PATH . '/api/notification/ios/IOSGroupcast.php');

class push_sender
{
    protected $appkey = NULL;
    protected $appMasterSecret = NULL;
    protected $timestamp = NULL;
    protected $validation_token = NULL;

    protected $android_alias_type = 'LRLZ';
    protected $androd_appkey = '5631efd4e0f55a8770000027';
    protected $androd_appmastersecret = 'r6w2a8z9x8zonh7qmk8ds2fvypu02wpj';

    protected $ios_alias_type = 'kPandaMakeUpAlias';
    protected $ios_appkey = '568b6727e0f55a06580011e7';
    protected $ios_appmastersecret = 'q9rmi5zjvfpvkwauenbgvhnmdsqdjeso';

    function __construct()
    {
        $this->timestamp = strval(time());
    }

    private function sendAndroidCustomizedcast(array $pushinfo)
    {
        $customizedcast = new AndroidCustomizedcast();
        $customizedcast->setAppMasterSecret($this->androd_appmastersecret);
        $customizedcast->setPredefinedKeyValue("appkey", $this->androd_appkey);
        $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
        $customizedcast->setPredefinedKeyValue("alias_type", $this->android_alias_type);
        $customizedcast->setPredefinedKeyValue("after_open", "go_app");

        $pre_keys = ['alias','title','text','img'];
        foreach ($pushinfo as $key => $value)
        {
            if(in_array($key,$pre_keys))
            {
                $customizedcast->setPredefinedKeyValue($key,$value);
                if($key == 'title') {
                    $customizedcast->setPredefinedKeyValue('ticker',$value);
                }
            }
            elseif( $key == 'extra')
            {
                if(empty($value) || is_array($value) == false) continue;

                foreach ($value as $skey => $sval) {
                    $customizedcast->setExtraField($skey, $sval);
                }
            }
        }

        return $customizedcast->send();
    }

    private function sendIOSCustomizedcast(array $pushinfo)
    {
        $customizedcast = new IOSCustomizedcast();
        $customizedcast->setAppMasterSecret($this->ios_appmastersecret);
        $customizedcast->setPredefinedKeyValue("appkey", $this->ios_appkey);
        $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
        $customizedcast->setPredefinedKeyValue("alias_type", $this->ios_alias_type);

        $customizedcast->setPredefinedKeyValue("badge", 0);
        $customizedcast->setPredefinedKeyValue("sound", "chime");
        if(is_publish()) {
            $customizedcast->setPredefinedKeyValue("production_mode", "true");
        } else {
            $customizedcast->setPredefinedKeyValue("production_mode", "false");
        }

        $pre_keys = ['alias','text'];
        foreach ($pushinfo as $key => $value)
        {
            if(in_array($key,$pre_keys))
            {
                if($key == 'text') {
                    $customizedcast->setPredefinedKeyValue('alert',$value);
                } else {
                    $customizedcast->setPredefinedKeyValue($key,$value);
                }
            }
            elseif( $key == 'extra')
            {
                if(empty($value) || is_array($value) == false) continue;

                foreach ($value as $skey => $sval) {
                    $customizedcast->setCustomizedField($skey, $sval);
                }
            }
            else {
                continue;
            }
        }

        return $customizedcast->send();
    }

    private function sendAndroidCustomizedcastMessage(array $pushinfo)
    {
        $customizedcast = new AndroidCustomizedcast();
        $customizedcast->setAppMasterSecret($this->androd_appmastersecret);
        $customizedcast->setPredefinedKeyValue("appkey", $this->androd_appkey);
        $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
        $customizedcast->setPredefinedKeyValue("alias_type", $this->android_alias_type);

        $customizedcast->setPredefinedKeyValue("display_type", 'message');
        if(is_publish()) {
            $customizedcast->setPredefinedKeyValue("production_mode", "true");
        } else {
            $customizedcast->setPredefinedKeyValue("production_mode", "false");
        }
        $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
        $customizedcast->setPredefinedKeyValue("custom", json_encode($pushinfo['custom']));
        return $customizedcast->send();
    }

    private function sendIOSCustomizedcastMessage(array $pushinfo)
    {
        $customizedcast = new IOSCustomizedcast();
        $customizedcast->setAppMasterSecret($this->ios_appmastersecret);
        $customizedcast->setPredefinedKeyValue("appkey", $this->ios_appkey);
        $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
        $customizedcast->setPredefinedKeyValue("alias_type", $this->ios_alias_type);
        $customizedcast->setPredefinedKeyValue("badge", 1);
        if(is_publish()) {
            $customizedcast->setPredefinedKeyValue("production_mode", "true");
        } else {
            $customizedcast->setPredefinedKeyValue("production_mode", "false");
        }
        $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
        $customizedcast->setPredefinedKeyValue("alert", $pushinfo['text']);
        $customizedcast->setCustomizedField("custom", json_encode($pushinfo['custom']));

        return $customizedcast->send();
    }

    public function send(array $info,$client_type = 0)
    {
        $fAndroid = false;
        $fIos = false;

        try
        {
            if($client_type == 0 || $client_type == session_helper::device_android)
            {
                $ret = $this->sendAndroidCustomizedcast($info);
                $data = json_decode($ret,true);
                if(strtoupper($data['ret']) == 'SUCCESS') {
                    $fAndroid = true;
                }
            }
        }
        catch (Exception $ex) {
            Log::record("push_app send Android error",Log::DEBUG);
        }

        try
        {
            if($client_type == 0 || $client_type == session_helper::device_ios)
            {
                $ret = $this->sendIOSCustomizedcast($info);
                $data = json_decode($ret,true);
                if(strtoupper($data['ret']) == 'SUCCESS') {
                    $fIos = true;
                }
            }
        }
        catch (Exception $ex) {
            Log::record("push_app send IOS error",Log::ERR);
        }

        return ($fAndroid | $fIos);
    }

    public function send_message(array $info,$client_type = 0)
    {
        $fAndroid = false;
        $fIos = false;

        try
        {
            if($client_type == 0 || $client_type == session_helper::device_android)
            {
                $ret = $this->sendAndroidCustomizedcastMessage($info);
                $data = json_decode($ret,true);
                if(strtoupper($data['ret']) == 'SUCCESS') {
                    $fAndroid = true;
                }
            }
        }
        catch (Exception $ex) {
            Log::record("push_app sendAndroid error",Log::ERR);
        }

        try
        {
            if($client_type == 0 || $client_type == session_helper::device_ios)
            {
                $ret = $this->sendIOSCustomizedcastMessage($info);
                $data = json_decode($ret,true);
                if(strtoupper($data['ret']) == 'SUCCESS') {
                    $fIos = true;
                }
            }
        }
        catch (Exception $ex) {
            Log::record("push_app sendAndroid error",Log::ERR);
        }

        return ($fAndroid || $fIos);
    }
}