package io.openim.android.ouicontact.ui;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

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

import com.alibaba.android.arouter.launcher.ARouter;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import io.openim.android.ouicontact.databinding.ActivityMyGroupBinding;
import io.openim.android.ouicontact.ui.search.SearchGroupActivity;
import io.openim.android.ouicore.adapter.RecyclerViewAdapter;
import io.openim.android.ouicore.adapter.ViewHol;
import io.openim.android.ouicore.base.BaseActivity;
import io.openim.android.ouicore.base.BaseApp;
import io.openim.android.ouicore.base.vm.injection.Easy;
import io.openim.android.ouicore.ex.MultipleChoice;
import io.openim.android.ouicore.utils.Constants;
import io.openim.android.ouicore.utils.Obs;
import io.openim.android.ouicore.utils.Routes;
import io.openim.android.ouicore.vm.GroupVM;
import io.openim.android.ouicore.vm.SelectTargetVM;
import io.openim.android.ouicore.vm.SocialityVM;
import io.openim.android.sdk.models.FriendInfo;
import io.openim.android.sdk.models.GroupInfo;

/**
 * 我的群组
 */
public class MyGroupActivity extends BaseActivity<SocialityVM, ActivityMyGroupBinding> implements Observer {

    private RecyclerViewAdapter<GroupInfo, ViewHol.GroupViewHo> joinedAdapter, createAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bindVM(SocialityVM.class);
        Obs.inst().addObserver(this);
        super.onCreate(savedInstanceState);
        bindViewDataBinding(ActivityMyGroupBinding.inflate(getLayoutInflater()));
        sink();
        initView();
        listener();
        vm.getAllGroup();
    }

    private void initView() {
        view.create.setLayoutManager(new LinearLayoutManager(this));
        view.joined.setLayoutManager(new LinearLayoutManager(this));

        joinedAdapter = new ContentAdapter(ViewHol.GroupViewHo.class);
        view.joined.setAdapter(joinedAdapter);

        createAdapter = new ContentAdapter(ViewHol.GroupViewHo.class);
        view.create.setAdapter(createAdapter);
    }

    private void listener() {
        view.searchView.setOnClickListener(v -> {
            SearchGroupActivity.jumpThis(this, vm.groups.getValue());
        });
        view.toCreate.setOnClickListener(v -> {
            SelectTargetVM targetVM = Easy.installVM(SelectTargetVM.class)
                    .setIntention(SelectTargetVM.Intention.isCreateGroup);
            targetVM.setOnFinishListener(() -> {
                GroupVM groupVM = BaseApp.inst().getVMByCache(GroupVM.class);
                if (null == groupVM)
                    groupVM = new GroupVM();
                groupVM.selectedFriendInfo.getValue().clear();
                List<MultipleChoice> multipleChoices = targetVM.metaData.getValue();
                for (int i = 0; i < multipleChoices.size(); i++) {
                    MultipleChoice us = multipleChoices.get(i);
                    FriendInfo friendInfo = new FriendInfo();
                    friendInfo.setUserID(us.key);
                    friendInfo.setNickname(us.name);
                    friendInfo.setFaceURL(us.icon);
                    groupVM.selectedFriendInfo.getValue().add(friendInfo);
                }
                BaseApp.inst().putVM(groupVM);
                ARouter.getInstance().build(Routes.Group.CREATE_GROUP2).navigation();
            });
            ARouter.getInstance().build(Routes.Group.SELECT_TARGET).navigation();
        });

        vm.groups.observe(this, groupInfos -> {
            joinedAdapter.setItems(groupInfos);
        });
        vm.ownGroups.observe(this, groupInfos -> {
            createAdapter.setItems(groupInfos);
            view.empty.setVisibility(createAdapter.getItems().isEmpty() ? View.VISIBLE : View.GONE);
        });

        view.menuGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == view.men1.getId()) {
                view.men1.setTextColor(Color.parseColor("#282828"));
                view.men1.setTypeface(null, Typeface.BOLD);
                view.men2.setTextColor(Color.parseColor("#656565"));
                view.men2.setTypeface(null, Typeface.NORMAL);
                view.menBg1.setVisibility(View.VISIBLE);
                view.menBg2.setVisibility(View.GONE);
                view.create.setVisibility(View.VISIBLE);
                view.joined.setVisibility(View.GONE);
                view.empty.setVisibility(createAdapter.getItems().isEmpty() ? View.VISIBLE : View.GONE);
            } else {
                view.men2.setTextColor(Color.parseColor("#282828"));
                view.men2.setTypeface(null, Typeface.BOLD);
                view.men1.setTextColor(Color.parseColor("#656565"));
                view.men1.setTypeface(null, Typeface.NORMAL);
                view.menBg2.setVisibility(View.VISIBLE);
                view.menBg1.setVisibility(View.GONE);
                view.empty.setVisibility(View.GONE);
                view.create.setVisibility(View.GONE);
                view.joined.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void update(Observable o, Object arg) {
        Obs.Message message = (Obs.Message) arg;
        if (message.tag == Constants.Event.DISSOLVE_GROUP) {
            vm.groups.getValue().clear();
            vm.ownGroups.getValue().clear();
            vm.getAllGroup();
        }
    }

    public class ContentAdapter extends RecyclerViewAdapter<GroupInfo, ViewHol.GroupViewHo> {

        public ContentAdapter(Class<ViewHol.GroupViewHo> viewHolder) {
            super(viewHolder);
        }

        @Override
        public void onBindView(@NonNull ViewHol.GroupViewHo holder, GroupInfo data, int position) {
            holder.view.avatar.load(data.getFaceURL(), true);
            holder.view.title.setText(data.getGroupName());
            holder.view.description.setText(data.getMemberCount() + MyGroupActivity.this.getString(io.openim.android.ouicore.R.string.people));

            holder.view.getRoot().setOnClickListener(v -> {
                ARouter.getInstance().build(Routes.Conversation.CHAT).withString(Constants.K_GROUP_ID, data.getGroupID()).withString(Constants.K_NAME, data.getGroupName()).navigation();
            });
        }
    }

    @Override
    protected void fasterDestroy() {
        super.fasterDestroy();
        Obs.inst().deleteObserver(this);
    }
}
