package com.setqq.script.sdk;

import java.util.ArrayList;
import java.util.HashMap;

import com.saki.log.SQLog;
import com.saki.qq.global.Global;
import com.saki.qq.global.TroopManager;
import com.saki.qq.wtlogin.pack.jce.Member;
import com.saki.qq.wtlogin.pack.jce.Troop;
import com.saki.service.NewService;

import com.setqq.script.Msg;

public class ScriptSend implements SendInterface {

    private NewService context;

    public ScriptSend(NewService context) {
        this.context = context;
    }

    @Override
    public Msg send(Msg msg) {
//        if (msg.type != Msg.TYPE_GROUP_MSG
//                && msg.getData().get("img") != null)
//            msg.getData().get("img").clear();
        switch (msg.type) {
            case Msg.TYPE_GROUP_MSG:
                context.sendMsg(-1,msg.groupid, -1, msg.getData());
                break;
            case Msg.TYPE_BUDDY_MSG:
                context.sendMsg(-1, -1, msg.uin, msg.getData());
                break;
            case Msg.TYPE_DIS_MSG:
                context.sendMsg(msg.groupid, -1, -1, msg.getData());
                break;
            case Msg.TYPE_SESS_MSG:
                if (msg.uin < 9999 || msg.code < 9999)
                    return null;
                context.sendMsg(-1, msg.code, msg.uin, msg.getData());
                break;
            case Msg.TYPE_SET_GROUP_SHUTUP:
                context.setShutup(msg.groupid, msg.value == 0);
                break;
            case Msg.TYPE_SET_MEMBER_SHUTUP:
                context.setShutup(msg.groupid, msg.uin, msg.value);
                break;
            case Msg.TYPE_FAVORITE:
                context.putFavorite(msg.uin);
                break;
            case Msg.TYPE_SET_MEMBER_CARD:
                context.setMemberCard(msg.groupid, msg.uin, msg.title);
                break;
            case Msg.TYPE_DELETE_MEMBER:
                context.deleteGroupMember(msg.groupid, msg.uin);
                break;
            case Msg.TYPE_GET_GROUP_LIST:
                ArrayList<Troop> list = TroopManager.getInstance().getTroopInfo();
                HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
                ArrayList<String> troop = new ArrayList<String>();
                for (Troop i : list) {
                    troop.add(i.id + "");
                }
                map.put("troop", troop);
                msg.setData(map);
                return msg;
            case Msg.TYPE_GET_GROUP_MEBMER:
                ArrayList<Member> m = TroopManager.getInstance().getMemberInfo(
                        msg.groupid);
                context.getGroupMember(msg.groupid);
                if (m != null) {
                    map = new HashMap<String, ArrayList<String>>();
                    ArrayList<String> member = new ArrayList<String>();

                    for (Member i : m) {
                        member.add(i.uin + "");
                    }
                    map.put("member", member);
                    msg.setData(map);
                    return msg;
                }
                return null;
            case Msg.TYPE_GET_LOGIN_ACCOUNT:
                msg.uin = Global.qq;
                return msg;
            case Msg.TYPE_AGREE_JOIN:
                context.agreeJoin(msg.groupid, msg.code, msg.uin, msg.value == 0);
                break;
            default:
                SQLog.i(msg.getTextMsg());
                break;
        }
        return null;
    }

}
