package net.tccn.redim.impl;

import com.jfinal.kit.Kv;
import net.tccn.bbs.base.BaseService;
import net.tccn.bbs.user.UserDetail;
import net.tccn.bbs.user.UserInfo;
import net.tccn.bbs.user.UserService;
import net.tccn.redim.service.ImFriendService;
import org.redkale.boot.Application;
import org.redkale.net.TransportFactory;
import org.redkale.net.TransportGroupInfo;
import org.redkale.net.http.RestService;
import org.redkale.service.RetResult;
import org.redkale.source.FilterExpress;
import org.redkale.source.FilterNode;
import org.redkale.util.ResourceType;

import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@ResourceType(net.tccn.redim.service.ImFriendService.class)
@RestService(name = "imfriend", automapping = true, comment = "好友管理")
public class ImFriendServiceImpl extends BaseService implements ImFriendService {

    @Resource
    private UserService userService;

    @Resource
    public static Application application;

    @Override
    public RetResult friends(String sessionid) {
        List<UserDetail> records = dataSource.queryList(UserDetail.class, FilterNode.create("status", FilterExpress.NOTEQUAL, -10));

        Kv data = Kv.create();

        //mine
        Kv mine = Kv.by("username", "游客").set("id", "0").set("sign", "").set("status", "online").set("avatar", "/res/images/avatar/13.jpg");
        UserInfo user = userService.current(sessionid);
        if (user != null) {
            mine.set("username", user.getNickname());
            mine.set("id", user.getUserid());
            mine.set("sign", user.getSign());
            mine.set("avatar", user.getAvatar());
            mine.set("status", "online");
        }

        //friend
        List<Kv> friend = new ArrayList<>();
        List<Kv> group = new ArrayList<>();

        List<Kv> list = new ArrayList();
        records.forEach(x -> {
            Kv _friend = Kv.by("id", x.getUserid());
            _friend.set("username", x.getNickname());
            _friend.set("sign", x.getSign());
            _friend.set("avatar", x.getAvatar());
            _friend.set("status", "online");
            list.add(_friend);
        });

        //把自己排在最上面
        List<Kv> _list = list.stream().sorted(Comparator.comparing(x -> !x.getStr("username").equals(mine.getStr("username")))).collect(Collectors.toList());

        friend.add(Kv.by("groupname", "默认分组").set("id", 1).set("online", _list.size()).set("list", _list));
        group.add(Kv.by("groupname", "默认分组").set("id", 1).set("avatar", "/res/images/avatar/13.jpg"));

        data.set("mine", mine).set("friend", friend).set("group", group);

        return RetResult.success(Kv.by("code", 0).set("msg", "").set("data", data));
    }

    @Override
    public RetResult friendList(String sessionid) {

        /*SncpClient sncpClient = new SncpClient("", ImFriendService.class, this, transportFactory,
                true, this.getClass(), new InetSocketAddress("192.168.227.1", 7070));*/


        /*TransportFactory sncpTransportFactory = application.getSncpTransportFactory();
        TransportGroupInfo groupInfo = sncpTransportFactory.findGroupInfo("ALL");

        System.out.println(groupInfo.toString());

        groupInfo.putAddress(new InetSocketAddress("120.24.230.60", 7070));*/


        //sncpTransportFactory.addGroupInfo(groupInfo);


        return friends(sessionid);
    }

    @Override
    public RetResult groups(String sessionid) {
        return null;
    }

    @Override
    public RetResult itemSave(String sessionid) {
        return null;
    }

    @Override
    public RetResult addFriend(String sessionid) {
        return null;
    }

    @Override
    public RetResult groupSave(String sessionid) {
        return null;
    }

    @Override
    public RetResult addGroup(String sessionid) {
        return null;
    }

}
