package io.openim.android.ouimoments.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;

import io.openim.android.ouicore.adapter.RecyclerViewAdapter;
import io.openim.android.ouicore.base.BaseActivity;
import io.openim.android.ouicore.utils.Constants;
import io.openim.android.ouicore.utils.Routes;
import io.openim.android.ouicore.utils.TimeUtil;
import io.openim.android.ouicore.widget.CommonDialog;
import io.openim.android.ouimoments.R;
import io.openim.android.ouimoments.adapter.viewholder.MsgDetailViewHolder;
import io.openim.android.ouimoments.bean.Comment;
import io.openim.android.ouimoments.bean.MomentsUser;
import io.openim.android.ouimoments.bean.WorkMoments;
import io.openim.android.ouimoments.databinding.ActivityMsgDetailBinding;
import io.openim.android.ouimoments.mvp.presenter.MsgDetailVM;
import io.openim.android.ouimoments.spannable.CircleMovementMethod;
import io.openim.android.ouimoments.spannable.SpannableClickable;
import io.openim.android.ouimoments.utils.DatasUtil;
import io.openim.android.ouimoments.utils.UrlUtils;

/**
 * 消息
 */
public class MsgDetailActivity extends BaseActivity<MsgDetailVM, ActivityMsgDetailBinding> {

    private RecyclerViewAdapter<WorkMoments, MsgDetailViewHolder> adapter;
    private int itemColor = Color.parseColor("#576B95");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bindVM(MsgDetailVM.class);
        super.onCreate(savedInstanceState);
        bindViewDataBinding(ActivityMsgDetailBinding.inflate(getLayoutInflater()));
        sink();
        initView();
        listener();
    }

    private void listener() {
        vm.workMomentsInfo.observe(this, workMoments -> {
            view.recyclerView.setVisibility(workMoments.isEmpty() ? View.GONE : View.VISIBLE);
            view.emptyLayout.setVisibility(workMoments.isEmpty() ? View.VISIBLE : View.GONE);
            adapter.setItems(workMoments);
        });
        view.clear.setOnClickListener(v -> {
            CommonDialog commonDialog = new CommonDialog(this).atShow();
            commonDialog.getMainView().tips.setText(io.openim.android.ouicore.R.string.clear_msg_sure);
            commonDialog.getMainView().cancel.setOnClickListener(v2 -> {
                commonDialog.dismiss();
            });
            commonDialog.getMainView().confirm.setOnClickListener(v2 -> {
                commonDialog.dismiss();
                vm.clearMsg(MsgDetailVM.msg_list);
            });

        });
    }

    @NonNull
    private SpannableString setClickableSpan(final String textStr, final String id) {
        SpannableString subjectSpanText = new SpannableString(textStr);
        subjectSpanText.setSpan(new SpannableClickable(itemColor) {
            @Override
            public void onClick(View widget) {
                //Toast.makeText(BaseApp.inst(), textStr + " &id = " + id, Toast.LENGTH_SHORT).show();
                ARouter.getInstance().build(Routes.Main.PERSON_DETAIL)
                        .withString(Constants.K_ID, id)
                        .navigation();
            }
        }, 0, subjectSpanText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return subjectSpanText;
    }

    private void initView() {
        vm.getWorkMomentsNotification();

        view.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        view.recyclerView.setAdapter(adapter = new RecyclerViewAdapter<WorkMoments, MsgDetailViewHolder>(MsgDetailViewHolder.class) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onBindView(@NonNull MsgDetailViewHolder holder, WorkMoments data, int position) {
                holder.view.getRoot().setOnClickListener(v -> {
                    startActivity(new Intent(MsgDetailActivity.this, MomentsDetailActivity.class)
                            .putExtra(Constants.K_ID, data.workMomentID));
                });
                holder.view.star.setVisibility(View.GONE);
                String nickName = "", faceURL = "";
                SpannableStringBuilder builder = new SpannableStringBuilder();
                final CircleMovementMethod circleMovementMethod = new CircleMovementMethod(getResources().getColor(R.color.praise_item_selector_default), getResources().getColor(R.color.praise_item_selector_default));
                if (data.type == MsgDetailVM.WorkMomentLogTypeComment) {
                    Comment comment = data.comments.get(0);
                    nickName = comment.nickname;
                    faceURL = comment.faceURL;
                    if (TextUtils.isEmpty(comment.replyUserID)) {
                        builder.append(getText(io.openim.android.ouicore.R.string.star_tips2));
                        builder.append(" ");
                        builder.append(setClickableSpan(data.nickname, data.userID));
                        builder.append(": ");
                        String contentBodyStr = comment.content;
                        builder.append(UrlUtils.formatUrlString(contentBodyStr));
                        holder.view.action.setText(builder);
//                        holder.view.action.setText(getText(io.openim.android.ouicore.R.string.star_tips2) + data.nickname + ":" + comment.content);
                    } else {
                        String targetUser = comment.replyNickname;
                        builder.append(getText(io.openim.android.ouicore.R.string.reply));
                        builder.append(" ");
                        builder.append(setClickableSpan(targetUser, comment.userID));
                        builder.append(": ");
                        String contentBodyStr = comment.content;
                        builder.append(UrlUtils.formatUrlString(contentBodyStr));
                        holder.view.action.setText(builder);
//                        holder.view.action.setText(getString(io.openim.android.ouicore.R.string.reply) + targetUser + ":" + comment.content);
                    }
                    holder.view.action.setMovementMethod(circleMovementMethod);
                }

                holder.view.time.setText(TimeUtil.getTime(data.createTime, TimeUtil.monthTimeFormat));
                if (data.type == MsgDetailVM.WorkMomentLogTypeLike) {
                    holder.view.star.setVisibility(View.VISIBLE);
                    MomentsUser user = data.likeUsers.get(0);
                    nickName = user.nickname;
                    faceURL = user.faceURL;
                    builder.append(getText(io.openim.android.ouicore.R.string.star_tips));
                    builder.append(" ");
                    builder.append(setClickableSpan(data.nickname, data.userID));
//                    String tips = getString(io.openim.android.ouicore.R.string.star_tips) + data.nickname;
                    holder.view.action.setText(builder);
                    holder.view.time.setText(TimeUtil.getTime(user.likeTime, TimeUtil.monthTimeFormat));
                }
                if (data.type == MsgDetailVM.WorkMomentLogTypeAt) {
                    nickName = data.nickname;
                    faceURL = data.faceURL;
                    builder.append(getText(io.openim.android.ouicore.R.string.about_you));
                    builder.append(" ");
                    builder.append(setClickableSpan(DatasUtil.curUser.getName(), DatasUtil.curUser.getId()));
//                    holder.view.action.setText(getString(io.openim.android.ouicore.R.string.about_you) + DatasUtil.curUser.getName());
                    holder.view.action.setText(builder);
                }
                holder.view.avatar.load(faceURL,nickName);
                holder.view.nickName.setText(nickName);

                if (data.content.metas.isEmpty()) {
                    holder.view.content.setVisibility(View.VISIBLE);
                    holder.view.media.setVisibility(View.GONE);
                    holder.view.content.setText(data.content.text);
                } else {
                    holder.view.media.setVisibility(View.VISIBLE);
                    holder.view.content.setVisibility(View.GONE);
                    String pictureUrl = data.content.metas.get(0).thumb;
                    if (TextUtils.isEmpty(pictureUrl))
                        pictureUrl = data.content.metas.get(0).original;
                    Glide.with(MsgDetailActivity.this).load(pictureUrl).into(holder.view.img);
                    holder.view.play.setVisibility(data.content.type != 0 ? View.VISIBLE : View.GONE);
                }
            }
        });

    }


}
