package io.openim.android.ouicontact.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

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

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.StringUtils;

import io.openim.android.ouicontact.R;
import io.openim.android.ouicontact.databinding.ActivityGroupNoticeListBinding;
import io.openim.android.ouicontact.databinding.ItemGroupNoticeBinding;
import io.openim.android.ouicontact.vm.ContactVM;
import io.openim.android.ouicore.adapter.RecyclerViewAdapter;
import io.openim.android.ouicore.base.BaseActivity;
import io.openim.android.ouicore.im.IMEvent;
import io.openim.android.sdk.models.GroupApplicationInfo;

/**
 * 新的群聊
 */
public class GroupNoticeListActivity extends BaseActivity<ContactVM, ActivityGroupNoticeListBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bindVM(ContactVM.class, true);
        super.onCreate(savedInstanceState);
        bindViewDataBinding(ActivityGroupNoticeListBinding.inflate(getLayoutInflater()));
        sink();

        initView();
        IMEvent.getInstance().addGroupListener(vm);

    }

    @Override
    protected void onResume() {
        super.onResume();
        vm.getRecvGroupApplicationList();
    }

    public void toBack(View v) {
        finish();
    }

    private void initView() {
        view.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter<GroupApplicationInfo, RecyclerViewItem>(RecyclerViewItem.class) {
            @Override
            public void onBindView(@NonNull RecyclerViewItem holder, GroupApplicationInfo data, int position) {
                LogUtils.e(data.toString());
//                holder.v.avatar.load(data.getUserFaceURL(),true);
                holder.v.avatar.load("", true);
                holder.v.groupName.setText(data.getGroupName());
                if (StringUtils.isEmpty(data.getInviterUserID())) {
                    SpanUtils.with(holder.v.hil)
                            .append(data.getNickname())
                            .setForegroundColor(Color.parseColor("#576B95"))
                            .append(getString(io.openim.android.ouicore.R.string.apply))
                            .append(getString(io.openim.android.ouicore.R.string.join_group))
                            .append(StringUtils.isEmpty(data.getReqMsg()) ? "" : "\n" + getString(io.openim.android.ouicore.R.string.reason))
                            .append(StringUtils.isEmpty(data.getReqMsg()) ? "" : data.getReqMsg())
                            .create();
                } else {
                    SpanUtils.with(holder.v.hil)
                            .append(data.getInviterUserID())
                            .setForegroundColor(Color.parseColor("#576B95"))
                            .append(getString(io.openim.android.ouicore.R.string.invite))
                            .append(data.getNickname())
                            .setForegroundColor(Color.parseColor("#576B95"))
                            .append(getString(io.openim.android.ouicore.R.string.join_group))
                            .create();
                }


                holder.v.getRoot().setOnClickListener(null);
                holder.v.handle.setBackgroundColor(getResources().getColor(android.R.color.white));
                if (data.getHandleResult() == 0) {
                    holder.v.handle.setBackgroundResource(io.openim.android.ouicore.R.drawable.sty_radius_4_f2f2f2);
                    holder.v.handle.setText(getString(io.openim.android.ouicore.R.string.accept));

                    holder.v.getRoot().setOnClickListener(v -> {
                        vm.groupDetail.setValue(data);
                        if (StringUtils.isEmpty(data.getInviterUserID())) {
                            startActivity(new Intent(GroupNoticeListActivity.this, GroupNoticeDetailActivity.class));
                        } else {
                            startActivity(new Intent(GroupNoticeListActivity.this, GroupNoticeInvitedDetailActivity.class));
                        }
                    });
                } else if (data.getHandleResult() == -1) {
                    holder.v.handle.setText(getString(io.openim.android.ouicore.R.string.declined));
                    holder.v.handle.setTextColor(Color.parseColor("#737373"));
                    Drawable icon = getResources().getDrawable(R.mipmap.ic_add_friend_arrow);
                    holder.v.handle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
                } else {
                    holder.v.handle.setText(getString(io.openim.android.ouicore.R.string.agreeed));
                    holder.v.handle.setTextColor(Color.parseColor("#737373"));
                    Drawable icon = getResources().getDrawable(R.mipmap.ic_add_friend_arrow);
                    holder.v.handle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
                }
            }
        };
        view.recyclerView.setAdapter(recyclerViewAdapter);

        vm.groupApply.observe(this, v -> recyclerViewAdapter.setItems(v));
    }

    public static class RecyclerViewItem extends RecyclerView.ViewHolder {
        public ItemGroupNoticeBinding v;

        public RecyclerViewItem(@NonNull View parent) {
            super((ItemGroupNoticeBinding.inflate(LayoutInflater.from(parent.getContext()))).getRoot());
            v = ItemGroupNoticeBinding.bind(itemView);
        }
    }

    @Override
    protected void fasterDestroy() {
        super.fasterDestroy();
        IMEvent.getInstance().removeGroupListener(vm);
        removeCacheVM();
    }
}
