<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/6/23
 * Time: 下午4:26
 */

require_once (BASE_ROOT_PATH . '/helper/relation/mem_relation.php');
require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
require_once (BASE_ROOT_PATH . '/helper/model_helper.php');

class relation_helper
{
    static public function onLogin($member_id)
    {
        try
        {
            $relation = new relation\mem_relation($member_id);
            $relation->pass_subscribe();
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("{$msg} member_id = {$member_id}");
        }
    }

    static public function onRegister($user)
    {
        try
        {
            $relation = new relation\mem_relation($user);
            $relation->pass_subscribe();
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("{$msg} member_id = {$user}");
        }
    }

    static public function onUpContacts($member_id,$contacts)
    {
        try
        {
            $relation = new relation\mem_relation($member_id);
            $relation->subscribe_contacts($contacts);
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("{$msg} member_id = {$member_id}");
        }
    }

    static public function onInvite($me_id,$user_id)
    {
        try
        {
            if($me_id == $user_id) {
                return false;
            }
            $relation = new relation\mem_relation($me_id);
            return $relation->invite($user_id);
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("onInvite: {$msg} param = {$me_id},{$user_id}");
            return false;
        }
    }

    static public function subscribed($me_id,$someone_id)
    {
        try
        {
            $me_id      = intval($me_id);
            $someone_id = intval($someone_id);
            if($me_id == $someone_id) {
                return false;
            }
            $relation = new relation\mem_relation($me_id);
            return $relation->is_follower($someone_id);
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("onInvite: {$msg} param = {$me_id},{$someone_id}");
            return false;
        }
    }

    static public function onSubscribe($me_id,$someone_id)
    {
        try
        {
            $me_id      = intval($me_id);
            $someone_id = intval($someone_id);
            if($me_id == $someone_id) {
                return false;
            }
            $relation = new relation\mem_relation($me_id);
            $ret = $relation->subscribe($someone_id);
            if($ret == true)
            {
                $someone_relation = new relation\mem_relation($someone_id);
                $someone_relation->pass_subscribe();

                $publisher = new message\publisher();
                $publisher->add_follow($someone_id,[$me_id]);
                $minfo = new member_info($me_id);
                push_helper::notice_subscribe($someone_id,$minfo);
            }
            return $ret;
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("onInvite: {$msg} param = {$me_id},{$someone_id}");
            return false;
        }
    }

    static public function onUnSubscribe($me_id,$someone_id)
    {
        try
        {
            if($me_id == $someone_id) {
                return false;
            }
            $relation = new relation\mem_relation($me_id);
            $ret = $relation->unsubscribe($someone_id);

            if($ret == true) {
                $publisher = new message\publisher();
                $publisher->del_follow($someone_id,[$me_id]);
            }
            return $ret;
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("onInvite: {$msg} param = {$me_id},{$someone_id}");
            return false;
        }
    }

    //关注我的
    static public function subscriber($member_id)
    {
        try
        {
            $relation = new relation\mem_relation($member_id);
            return $relation->subscriber();
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("{$msg} member_id = {$member_id}");
            return array();
        }
    }

    static public function follower($member_id)
    {
        try
        {
            $relation = new relation\mem_relation($member_id);
            return $relation->follower();
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("{$msg} member_id = {$member_id}");
            return [];
        }
    }

    static public function friends($member_id)
    {
        try
        {
            $relation = new relation\mem_relation($member_id);
            $subscriber = $relation->subscriber();
            $follower = $relation->follower();

            return array_merge($subscriber,$follower);
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("{$msg} member_id = {$member_id}");
            return [];
        }
    }
    static public function is_follow($me,$other)
    {
        try
        {
            $relation = new relation\mem_relation($me);
            return $relation->is_follower($other);
        }
        catch (Exception $ex)
        {
            $msg = $ex->getMessage();
            Log::record("is_follow {$msg} member_id = {$me}");
            return false;
        }
    }
}