package com.bc_chat.im.redpackage;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;

import com.bc_chat.im.R;
import com.bc_chat.im.activity.envelopes.SendGroupEnvelopesActivity;
import com.jrmf360.rylib.rp.extend.CurrentUser;

import io.rong.imkit.RongExtension;
import io.rong.imkit.RongIM;
import io.rong.imkit.plugin.IPluginModule;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

public class MGroupRedPackagePlugin implements IPluginModule {

    private static final int REQUEST_GROUPREDPACKAGE = 51;
    private Conversation.ConversationType conversationType;
    private String targetId;

    @Override
    public Drawable obtainDrawable(Context context) {
        return ContextCompat.getDrawable(context, R.drawable.selector_hongbao);
    }

    @Override
    public String obtainTitle(Context context) {
        return context.getString(R.string.hongbao);
    }

    @Override
    public void onClick(Fragment fragment, RongExtension rongExtension) {
        this.conversationType = rongExtension.getConversationType();
        this.targetId = rongExtension.getTargetId();
        Intent var3 = new Intent(rongExtension.getContext(), SendGroupEnvelopesActivity.class);
        var3.putExtra("TargetId", rongExtension.getTargetId());
        var3.putExtra("user_id", CurrentUser.getUserId());
        var3.putExtra("user_name", CurrentUser.getName());
        var3.putExtra("user_icon", CurrentUser.getUserIcon());
        rongExtension.startActivityForPluginResult(var3, REQUEST_GROUPREDPACKAGE, this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == -1) {
            sendRpMessage(data);
        }
    }

    private void sendRpMessage(Intent data) {
        String envelopesID = data.getStringExtra("envelopesID");
        String envelopeMessage = data.getStringExtra("envelopeMessage");
        String envelopeName = data.getStringExtra("envelopeName");
        if (RongIM.getInstance() != null && RongIM.getInstance().getRongIMClient() != null) {
            RCRedPackageMessage redPackageMessage = RCRedPackageMessage.obtain(envelopesID, envelopeName, envelopeMessage, "[" + envelopeName + "] " + envelopeMessage);
            RongIM.getInstance().sendMessage(Message.obtain(targetId, conversationType, redPackageMessage),
                    "您收到了一条消息", null, new IRongCallback.ISendMessageCallback() {
                        @Override
                        public void onAttached(Message message) {

                        }

                        @Override
                        public void onSuccess(Message message) {

                        }

                        @Override
                        public void onError(Message message, RongIMClient.ErrorCode errorCode) {

                        }
                    });

        }
    }
}
