package com.common.im.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.common.im.contract.SetRemarksContract;
import com.common.im.presenter.SetRemarksPresenter;
import com.common.im_ui.R;
import com.common.im_ui.databinding.ActivitySetRemarkBinding;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.LogUtils;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;

import androidx.annotation.Nullable;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Group;

/**
 * 创建日期：2022/6/13 11:15
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.ChatCenter.CHAT_GROUP_SET_REMARK)
public class SetRemarksActivity extends BaseMVPActivity<ActivitySetRemarkBinding, SetRemarksPresenter> implements SetRemarksContract.SetRemarksView, View.OnClickListener {
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == com.cooleshow.base.R.id.tv_right_text) {
            remarks = et_remarks.getText().toString().trim();
            if (TextUtils.isEmpty(remarks)) {
                ToastUtil.getInstance().showShort("请输入备注名");
                return;
            }
            presenter.updateGroupInfo(targetId, remarks);
        } else if (id == R.id.iv_close) {
            et_remarks.setText("");
        }
    }

    private EditText et_remarks;
    private String targetId;
    private String remarks;
    private ImageView iv_close;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "设置备注名");
        viewBinding.toolbarInclude.tvRightText.setText("完成");
        viewBinding.toolbarInclude.tvRightText.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
        viewBinding.toolbarInclude.tvRightText.setVisibility(View.VISIBLE);
        viewBinding.toolbarInclude.tvRightText.setOnClickListener(this);
        et_remarks = viewBinding.etRemarks;
        iv_close = viewBinding.ivClose;
        iv_close.setOnClickListener(this);
        Intent intent = getIntent();
        targetId = intent.getStringExtra("targetId");
        remarks = intent.getStringExtra("remarks");
        et_remarks.setText(remarks);
        et_remarks.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) {
                if (et_remarks.getText().toString().length() > 0) {
                    iv_close.setVisibility(View.VISIBLE);
                } else {
                    iv_close.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected ActivitySetRemarkBinding getLayoutView() {
        return ActivitySetRemarkBinding.inflate(getLayoutInflater());
    }

    @Override
    protected SetRemarksPresenter createPresenter() {
        return new SetRemarksPresenter();
    }

    @Override
    public void updateGroupInfoSuccess(String targetId, String title) {
        if (!TextUtils.isEmpty(targetId) && !TextUtils.isEmpty(title)) {
            RongIM.getInstance().refreshGroupInfoCache(new Group(targetId, title, Uri.parse("")));
            RongIMClient.getInstance().updateConversationInfo(Conversation.ConversationType.GROUP, targetId, title, title, new RongIMClient.ResultCallback() {
                @Override
                public void onSuccess(Object o) {
                    LogUtils.i("pq", "设置成功");

                }

                @Override
                public void onError(RongIMClient.ErrorCode e) {
                    LogUtils.i("pq", "设置fail:" + e.msg + "\n" + "code:" + e.code);
                }
            });
        }
        ToastUtil.getInstance().showShort("设置成功");
        Intent intent = new Intent();
        intent.putExtra("remarks", remarks);
        setResult(RESULT_OK, intent);
        finish();
    }
}
