package com.bc_chat.im.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bc_chat.im.R;
import com.bc_chat.im.messages.vibration.RCVibrationMessage;

import io.rong.imkit.RongBaseNoActionbarActivity;
import io.rong.imkit.RongIM;
import io.rong.imkit.emoticon.AndroidEmoji;
import io.rong.imkit.userInfoCache.RongUserInfoManager;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Group;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;

/**
 * Created by Beyond on 2016/11/24.
 */

public class SendVibrationDetailActivity extends RongBaseNoActionbarActivity {

    private EditText mMessage;
    private TextView mSend;
    private TextView mCancel;
    private TextView tvTitle;
    private TextView tvTextCount;

    private Conversation.ConversationType mConversationType;
    private String mTargetId;
    private Group group;
    private String defaultText = "震";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.rc_ac_vibration_detail);
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        initView();
        initData();
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTextCount = (TextView) findViewById(R.id.tv_text_count);

        mMessage = (EditText) findViewById(R.id.message);
        mSend = (TextView) findViewById(R.id.send);
        mCancel = (TextView) findViewById(R.id.cancel);
        mCancel.requestFocus();
        this.setFinishOnTouchOutside(false);
    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        mTargetId = getIntent().getStringExtra("targetId");
        mConversationType = (Conversation.ConversationType) getIntent().getSerializableExtra("conversationType");

        switch (mConversationType) {
            case PRIVATE:
                UserInfo mine = RongUserInfoManager.getInstance().getUserInfo(mTargetId);
                defaultText = "";
                tvTitle.setText("您要震" + mine.getName() + "上线吗？");
                break;
            case ENCRYPTED:
                String userId = null;
                String str[] = mTargetId.split(";;;");
                if (str.length >= 2) {
                    userId = str[1];
                }
                mine = RongUserInfoManager.getInstance().getUserInfo(userId);
                defaultText = "";
                tvTitle.setText("您要震" + mine.getName() + "上线吗？");
                break;
            case GROUP:
                group = RongUserInfoManager.getInstance().getGroupInfo(mTargetId);
                defaultText = "";
                tvTitle.setText("您要震群内所有人吗？");
                break;
            default:
                break;
        }

        mMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null) {
                    int start = mMessage.getSelectionStart();
                    int end = mMessage.getSelectionEnd();
                    mMessage.removeTextChangedListener(this);
                    mMessage.setText(AndroidEmoji.ensure(s.toString()));
                    mMessage.addTextChangedListener(this);
                    mMessage.setSelection(start, end);
                    tvTextCount.setText(30 - s.length() + "");
                } else {
                    tvTextCount.setText("30");
                }
            }
        });

        mSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfo sendUserInfo = RongUserInfoManager.getInstance().getUserInfo(RongIMClient.getInstance().getCurrentUserId());
                String friendPortrait = sendUserInfo.getPortraitUri() == null ? "" : sendUserInfo.getPortraitUri().toString();
                String sendUserName = sendUserInfo == null ? "" : sendUserInfo.getName();
                RCVibrationMessage vibrationMessage = RCVibrationMessage.obtain(TextUtils.isEmpty(mMessage.getText()) ? defaultText : mMessage.getText().toString());
                RongIM.getInstance().sendMessage(Message.obtain(mTargetId, mConversationType, vibrationMessage),
                        sendUserName + "正在震你上线", 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) {

                            }
                        });
                hideInputKeyBoard();
                finish();
            }
        });

        mCancel.setOnClickListener(v -> {
            hideInputKeyBoard();
            finish();
        });
    }


    private void hideInputKeyBoard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mMessage.getWindowToken(), 0);
    }

    @Override
    public void onBackPressed() {

    }
}
