package jiguang.chat.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;

import cn.jpush.im.android.api.ChatRoomManager;
import cn.jpush.im.android.api.callback.RequestCallback;
import cn.jpush.im.android.api.model.ChatRoomInfo;
import jiguang.chat.R;
import jiguang.chat.utils.CommonUtils;

/**
 * Created by ${chenyn} on 2017/11/9.
 */

public class SearchChatRoomActivity extends BaseActivity {

    private LinearLayout mLl_chatRoomItem;
    private ImageView mIv_chatRoomAvatar;
    private TextView mTv_chatRoomName;
    private TextView mTv_chatRoomDesc;
    private EditText mSearchEditText;
    private LinearLayout mAc_iv_press_back;
    private TextView mTv_search;
    private long mRoomID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_search_chat_room);

        initView();
        initData();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initData() {
        //清空EditText
        mSearchEditText.setOnTouchListener((v, event) -> {
            final int DRAWABLE_RIGHT = 2;
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (event.getRawX() >= (mSearchEditText.getRight() - 2 * mSearchEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    mSearchEditText.setText("");
                    mSearchEditText.clearFocus();
                    mLl_chatRoomItem.setVisibility(View.GONE);
                    return true;
                }
            }
            return false;
        });

        mAc_iv_press_back.setOnClickListener(v -> {
            finish();
            CommonUtils.hideKeyboard(SearchChatRoomActivity.this);
        });
        mTv_search.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(mSearchEditText.getText())) {
                String roomId = mSearchEditText.getText().toString().trim();
                ChatRoomManager.getChatRoomInfos(Collections.singleton(Long.parseLong(roomId)), new RequestCallback<List<ChatRoomInfo>>() {
                    @Override
                    public void gotResult(int i, String s, List<ChatRoomInfo> chatRoomInfos) {
                        if (i == 0) {
                            mRoomID = chatRoomInfos.get(0).getRoomID();
                            mLl_chatRoomItem.setVisibility(View.VISIBLE);
                            mTv_chatRoomDesc.setText(chatRoomInfos.get(0).getDescription());
                            mTv_chatRoomName.setText(chatRoomInfos.get(0).getName());
                        }else {
                            mLl_chatRoomItem.setVisibility(View.GONE);
                            Toast.makeText(SearchChatRoomActivity.this, "搜索的聊天室不存在", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

        mLl_chatRoomItem.setOnClickListener(v -> {
            Intent intent = new Intent(SearchChatRoomActivity.this, ChatRoomDetailActivity.class);
            intent.putExtra("chatRoomId", mRoomID);
            startActivity(intent);
        });

    }

    private void initView() {
        mLl_chatRoomItem = (LinearLayout) findViewById(R.id.ll_chatRoomItem);
        mIv_chatRoomAvatar = (ImageView) findViewById(R.id.iv_chatRoomAvatar);
        mTv_chatRoomName = (TextView) findViewById(R.id.tv_chatRoomName);
        mTv_chatRoomDesc = (TextView) findViewById(R.id.tv_chatRoomDesc);
        mTv_search = (TextView) findViewById(R.id.tv_search);

        mAc_iv_press_back = (LinearLayout) findViewById(R.id.ac_iv_press_back);
        mSearchEditText = (EditText) findViewById(R.id.ac_et_search);
    }

}

@@ -2300,24 +2300,104 @@
 g().trim();%0A
+                try %7B%0A                    long id = Long.parseLong(roomId);%0A    
             
@@ -2455,38 +2455,18 @@
 ngleton(
-Long.parseLong(roomId)
+id
 ), new R
@@ -2520,24 +2520,28 @@
             
+  
+  
 @Override%0A  
@@ -2534,24 +2534,28 @@
   @Override%0A
+    
             
@@ -2633,24 +2633,28 @@
 oomInfos) %7B%0A
+    
             
@@ -2707,16 +2707,20 @@
         
+    
 mRoomID 
@@ -2751,24 +2751,28 @@
 etRoomID();%0A
+    
             
@@ -2853,32 +2853,36 @@
                 
+  
+  
 mTv_chatRoomDesc
@@ -2926,24 +2926,28 @@
 ription());%0A
+    
             
@@ -3044,16 +3044,20 @@
         
+    
 %7Delse %7B%0A
@@ -3076,32 +3076,36 @@
                 
+ 
+   
 mLl_chatRoomItem
@@ -3123,32 +3123,36 @@
 ity(View.GONE);%0A
+    
                 
@@ -3264,34 +3264,42 @@
                 
+   
+ 
 %7D%0A
+    
                 
@@ -3312,35 +3312,214 @@
                 
-%7D);
+    %7D);%0A                %7D catch (NumberFormatException e) %7B%0A                    Toast.makeText(SearchChatRoomActivity.this, %22%E6%90%9C%E7%B4%A2%E7%9A%84%E8%81%8A%E5%A4%A9%E5%AE%A4%E4%B8%8D%E5%AD%98%E5%9C%A8%22, Toast.LENGTH_SHORT).show();%0A                %7D
 %0A            %7D%0A 

修复搜索聊天室时输入聊天室ID过长造成的崩溃问题，IM-5391