<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/7/21
 * Time: 下午12:07
 */
require_once (BASE_ROOT_PATH . '/helper/relation_helper.php');
require_once (BASE_ROOT_PATH . '/helper/model_helper.php');
require_once (BASE_ROOT_PATH . '/helper/util_helper.php');

class member_relationControl extends mbMemberControl
{
    //subscriber TEXT COMMENT '订阅我为好友的人。',
    //follower TEXT COMMENT '我订阅别人成为好友通过的人。'
    public function __construct()
    {
        parent::__construct();
        relation_helper::onLogin($_SESSION['member_id']);
    }

    //我关注的人
    public function subscriberOp()
    {
        $iRelation = new relation\mem_relation($_SESSION['member_id']);
        $follower = $iRelation->follower();

        $mem_desc = array();
        $pages = 0;
        $follower_infos = array();

        if(!empty($follower))
        {
            $follower = self::separate_page($follower,$pages);
            $members = Model('member')->getMemberList(array('member_id' => array('in',$follower)));

            foreach ($members as $val) {
                $info = new member_info($val);
                array_push($mem_desc,$info->filter());
            }

            $f_infos = Model('member_relation')->getRelationList(array('member_id' => array('in',$follower)));
            foreach ($f_infos as $val)
            {
                $relation = new relation\mem_relation($val);
                $item = ['fans_count' =>$relation->subscriber_count(),
                         'member_id' => $relation->member_id(),
                         'subscribed' => true];
                array_push($follower_infos,$item);
            }
        }

        return self::outsuccess(array('subscribers' => $follower_infos,
            'count' => $iRelation->follower_count(),
            'mem_desc' => $mem_desc,
            'mobile_page' => mobile_page($pages)));
    }

    //我的粉丝
    public function followerOp()
    {
        $iRelation = new relation\mem_relation($_SESSION['member_id']);
        $follows = $iRelation->subscriber();

        $mem_desc = array();
        $pages = 0;
        $follower_infos = array();
        if(!empty($follows))
        {
            $follows = self::separate_page($follows,$pages);
            $members = Model('member')->getMemberList(array('member_id' => array('in',$follows)));
            foreach ($members as $val) {
                $info = new member_info($val);
                array_push($mem_desc,$info->filter());
            }
            $f_infos = Model('member_relation')->getRelationList(array('member_id' => array('in',$follows)));
            foreach ($f_infos as $val)
            {
                $relation = new relation\mem_relation($val);
                $item = [ 'fans_count' => $relation->subscriber_count(),
                          'member_id'  => $relation->member_id(),
                          'subscribed' => $iRelation->is_follower($relation->member_id())];
                array_push($follower_infos,$item);
            }
        }

        return self::outsuccess(['followers' => $follower_infos,
                                 'count' => $iRelation->subscriber_count(),
                                 'mem_desc' => $mem_desc,
                                 'mobile_page' => mobile_page($pages)]);
    }

    public function subscribeOp()
    {
        if(!isset($_GET['userid']) || empty($_GET['userid'])) {
            return self::outerr(errcode::ErrParamter,"需要传递userid参数.");
        }

        $userid = intval($_GET['userid']);
        if($userid <= 0) {
            return self::outerr(errcode::ErrParamter,"userid必须大于0.");
        }
        try
        {
            if(relation_helper::is_follow(session_helper::memberid(),$userid))
            {
                try
                {
                    if(relation_helper::onUnSubscribe(session_helper::memberid(),$userid) == true) {
                        return self::outsuccess(NULL);
                    } else{
                        return self::outerr(errcode::ErrRelation,"关注的用户不存在");
                    }
                } catch (Exception $ex) {
                    return self::outerr(errcode::ErrRelation,"关注的用户不存在");
                }
            }
            else
            {
                if(relation_helper::onSubscribe(session_helper::memberid(),$userid) == true) {
                    return self::outsuccess(NULL);
                } else{
                    return self::outerr(errcode::ErrRelation,"关注的用户不存在");
                }
            }
        }
        catch (Exception $ex) {
            return self::outerr(errcode::ErrRelation,"关注的用户不存在");
        }
    }

    public function unsubscribeOp()
    {
        if(!isset($_GET['userid']) || empty($_GET['userid'])) {
            return self::outerr(errcode::ErrParamter,"需要传递userid参数.");
        }

        $userid = intval($_GET['userid']);
        if($userid <= 0) {
            return self::outerr(errcode::ErrParamter,"userid必须大于0.");
        }
        try
        {
            if(relation_helper::is_follow($_SESSION['member_id'],$userid))
            {
                if(relation_helper::onUnSubscribe($_SESSION['member_id'],$userid) == true) {
                    return self::outsuccess(NULL);
                } else{
                    return self::outerr(errcode::ErrRelation,"关注的用户不存在");
                }
            }
            else
            {
                if(relation_helper::onSubscribe($_SESSION['member_id'],$userid) == true) {
                    return self::outsuccess(NULL);
                } else{
                    return self::outerr(errcode::ErrRelation,"关注的用户不存在");
                }
            }
        }
        catch (Exception $ex) {
            return self::outerr(errcode::ErrRelation,"关注的用户不存在");
        }
    }

    public function kinsOp()
    {
        $mem_desc = [];
        $kins = [];

        $invitor = $this->invitor();
        if($this->page_no() == 1 && !empty($invitor)) {
            $info = new member_info($invitor);
            $item = $info->filter();
            $item['is_invitor'] = true;

            $kin['fans_count'] = 3;
            $kin['member_id'] = $info->member_id();
            $kin['subscribed'] = true;
            $kins[] = $kin;

            $mem_desc [] = $item;
        }

        $mod_member = Model('member');
        $invitees = $mod_member->getMemberList(['inviter_id' => $_SESSION['member_id']],'*',$this->page_size());
        foreach ($invitees as $val) {
            $info = new member_info($val);
            $item = $info->filter();
            $item['is_invitor'] = true;
            $mem_desc [] = $item;

            $kin['fans_count'] = 3;
            $kin['member_id'] = $info->member_id();
            $kin['subscribed'] = true;
            $kins[] = $kin;
        }

        $pages = $mod_member->gettotalpage();
        $count = $mod_member->gettotalnum() + (empty($invitor) ? 0 : 1);

        return self::outsuccess(['count' => $count,
            'kins' => $kins,
            'mem_desc' => $mem_desc,
            'mobile_page' => mobile_page($pages)]);
    }

    private function invitor()
    {
        $mod_member = Model('member');
        if(isset($_SESSION['inviter_id']) == false) {
            $inviter_id = $mod_member->getMemberInfo(['member_id' => $_SESSION['member_id']],'inviter_id');
            $_SESSION['inviter_id'] = $inviter_id;
        }

        $inviter_id = $_SESSION['inviter_id'];
        if($inviter_id > 0) {
            $info = $mod_member->getMemberInfoByID($inviter_id);
            return $info;
        } else {
            return false;
        }
    }
}