package io.openim.android.ouigroup.ui.v3;

import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.core.LogisticsCenter;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.xiaofeng.flowlayoutmanager.Alignment;
import com.xiaofeng.flowlayoutmanager.FlowLayoutManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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.vm.injection.Easy;
import io.openim.android.ouicore.databinding.LayoutPopSelectedFriendsBinding;
import io.openim.android.ouicore.entity.ExUserInfo;
import io.openim.android.ouicore.ex.MultipleChoice;
import io.openim.android.ouicore.utils.Common;
import io.openim.android.ouicore.utils.Constants;
import io.openim.android.ouicore.utils.OnDedrepClickListener;
import io.openim.android.ouicore.utils.Routes;
import io.openim.android.ouicore.vm.SelectTargetVM;
import io.openim.android.ouicore.vm.SocialityVM;
import io.openim.android.ouicore.widget.AvatarImage;
import io.openim.android.ouicore.widget.StickyDecoration;
import io.openim.android.ouigroup.R;
import io.openim.android.ouigroup.databinding.ActivityCreateGroupSelectBinding;
import io.openim.android.sdk.models.FriendInfo;

/**
 * 群组选择好友
 */
@Route(path = Routes.Group.SELECT_TARGET_GROUP)
public class SelectTargetActivityGroup extends BaseActivity<SocialityVM, ActivityCreateGroupSelectBinding> {

    private SelectTargetVM selectTargetVM;
    private RecyclerViewAdapter<ExUserInfo, RecyclerView.ViewHolder> adapter;
    private LinearLayoutManager mLayoutManager;
    private String mLetters = "↑ABCDEFGHIJKLMNOPQRSTUVWXYZ#";//默认字符
    private SelectAvatarAdapter selectAvatarAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bindVM(SocialityVM.class);
        super.onCreate(savedInstanceState);
        bindViewDataBinding(ActivityCreateGroupSelectBinding.inflate(getLayoutInflater()));
        init();
        initView();
        vm.getAllFriend();
        click();
        listener();

    }

    private void click() {
        view.searchView.setOnClickListener(v -> {
            if (selectTargetVM.isSingleSelect()) {
                ARouter.getInstance().build(Routes.Contact.SEARCH_FRIENDS_GROUP)
                        .withBoolean(Constants.IS_SELECT_FRIEND, true)
                        .navigation();
                return;
            }

            Postcard postcard = ARouter.getInstance().build(Routes.Contact.SEARCH_FRIENDS_GROUP);
            LogisticsCenter.completion(postcard);
            launcher.launch(new Intent(this, postcard.getDestination())
                    .putExtra(Constants.K_RESULT, (Serializable) selectTargetVM.metaData.val())
                    .putExtra(Constants.IS_SELECT_FRIEND, selectTargetVM.isInvite()
                            || selectTargetVM.isMultipleSelectFriends() || selectTargetVM.isCreateGroup()));
        });
    }

    private void listener() {
        selectTargetVM.metaData.observe(this, v -> {
            if (null !=selectAvatarAdapter){
                view.selectRlv.setVisibility(v.isEmpty()?View.GONE:View.VISIBLE);
                selectAvatarAdapter.setDatas(v);
                selectAvatarAdapter.notifyDataSetChanged();
            }
            if (null != adapter){
                adapter.notifyDataSetChanged();
            }
        });
        view.sortView.setLetters(mLetters);
        vm.exUserInfo.observe(this, v -> {
            if (null == v || v.isEmpty()) return;
            List<ExUserInfo> exUserInfos = new ArrayList<>(v);
            adapter.setItems(exUserInfos);
        });
        view.sortView.setOnLetterChangedListener((letter, position) -> {
            if (letter.equals("↑")) {
                Common.smoothMoveToPosition(view.recyclerView, 0);
                return;
            }
            for (int i = 0; i < adapter.getItems().size(); i++) {
                ExUserInfo exUserInfo = adapter.getItems().get(i);
                if (exUserInfo.sortLetter.equalsIgnoreCase(letter)) {
                    Common.smoothMoveToPosition(view.recyclerView, i);
                    return;
                }
            }
        });
        view.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (Common.mShouldScroll && RecyclerView.SCROLL_STATE_IDLE == newState) {
                    Common.mShouldScroll = false;
                    Common.smoothMoveToPosition(view.recyclerView, Common.mToPosition);
                }
            }
        });
    }

    private void initView() {
        FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
        flowLayoutManager.setAutoMeasureEnabled(true);
        flowLayoutManager.setAlignment(Alignment.LEFT);
        view.selectRlv.setLayoutManager(flowLayoutManager);
        selectAvatarAdapter = new SelectAvatarAdapter();
        selectAvatarAdapter.setDatas(new ArrayList<>());
        view.selectRlv.setAdapter(selectAvatarAdapter);
        view.recyclerView.setLayoutManager(mLayoutManager = new LinearLayoutManager(this) {
            @Override
            public void smoothScrollToPosition(RecyclerView recyclerView,
                                               RecyclerView.State state, int position) {
                LinearSmoothScroller linearSmoothScroller =
                        new LinearSmoothScroller(recyclerView.getContext()) {
                            @Override
                            protected int calculateTimeForScrolling(int dx) {
                                // 此函数计算滚动dx的距离需要多久，当要滚动的距离很大时，比如说52000，
                                // 经测试，系统会多次调用此函数，每10000距离调一次，所以总的滚动时间
                                // 是多次调用此函数返回的时间的和，所以修改每次调用该函数时返回的时间的
                                // 大小就可以影响滚动需要的总时间，可以直接修改些函数的返回值，也可以修改
                                // dx的值，这里暂定使用后者.
                                // (See LinearSmoothScroller.TARGET_SEEK_SCROLL_DISTANCE_PX)
                                if (dx > 1500) {
                                    dx = 1500;
                                }
                                return super.calculateTimeForScrolling(dx);
                            }

                            @Override
                            public PointF computeScrollVectorForPosition(int targetPosition) {
                                return mLayoutManager.computeScrollVectorForPosition(targetPosition);
                            }
                        };
                linearSmoothScroller.setTargetPosition(position);
                startSmoothScroll(linearSmoothScroller);
            }
        });
        adapter = new RecyclerViewAdapter<ExUserInfo, RecyclerView.ViewHolder>() {

            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new ViewHol.ItemViewHo(parent);
            }

            @Override
            public void onBindView(@NonNull RecyclerView.ViewHolder holder, ExUserInfo data, int position) {
                ViewHol.ItemViewHo itemViewHo = (ViewHol.ItemViewHo) holder;
                FriendInfo friendInfo = data.userInfo.getFriendInfo();
                itemViewHo.view.avatar.load(friendInfo.getFaceURL(),friendInfo.getNickname());
                itemViewHo.view.nickName.setText(friendInfo.getNickname());

                MultipleChoice target = null;
                if (null != selectTargetVM && !selectTargetVM.isSingleSelect()) {
                    itemViewHo.view.select.setVisibility(View.VISIBLE);
                    itemViewHo.view.select.setChecked(selectTargetVM
                            .contains(new MultipleChoice(friendInfo.getUserID())));
                    int index = selectTargetVM.metaData.val()
                            .indexOf(new MultipleChoice(friendInfo.getUserID()));

                    if (index != -1) {
                        target = selectTargetVM.metaData.val().get(index);
                        itemViewHo.view.select.setEnabled(target.isEnabled);
                        itemViewHo.view.select.setAlpha(target.isEnabled ? 1f : 0.5f);
                    }
                }
                MultipleChoice finalTarget = target;
                itemViewHo.view.getRoot().setOnClickListener(v -> {
                    if (null != selectTargetVM) {
                        if (selectTargetVM.isSingleSelect()) {
                            selectTargetVM.addMetaData(friendInfo.getUserID(),
                                    friendInfo.getNickname(),
                                    friendInfo.getFaceURL());

                            selectTargetVM.toFinish();
                            return;
                        }
                        if (null != finalTarget && !finalTarget.isEnabled) return;
                        itemViewHo.view.select.setChecked(!itemViewHo.view.select.isChecked());

                        if (itemViewHo.view.select.isChecked()) {
                            MultipleChoice meta = new MultipleChoice(friendInfo.getUserID());
                            meta.name = friendInfo.getNickname();
                            meta.icon = friendInfo.getFaceURL();
                            meta.isSelect = true;
                            selectTargetVM.addDate(meta);
                        } else {
                            selectTargetVM.removeMetaData(friendInfo.getUserID());
                        }
                        return;
                    }
//                    ARouter.getInstance().build(Routes.Main.PERSON_DETAIL)
//                            .withString(Constants.K_ID, friendInfo.getUserID())
//                            .navigation(SelectTargetActivityGroup.this, 1001);
                });
            }
        };
        view.recyclerView.setAdapter(adapter);
        view.recyclerView.addItemDecoration(new StickyDecoration(this, position -> adapter.getItems().get(position).sortLetter));
    }

    private ActivityResultLauncher<Intent> launcher =
            registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), v -> {
                if (v.getResultCode() != RESULT_OK) return;
                Intent intent = v.getData();
                Set<MultipleChoice> set = (Set<MultipleChoice>) intent.getSerializableExtra(Constants.K_RESULT);
                for (MultipleChoice data : set) {
                    if (data.isSelect) {
                        if (!selectTargetVM.contains(data)) {
                            selectTargetVM.metaData.val().add(data);
                            selectTargetVM.metaData.update();
                        }
                    } else {
                        selectTargetVM.removeMetaData(data.key);
                    }
                }
            });

    void init() {
        selectTargetVM = Easy.find(SelectTargetVM.class);
        selectTargetVM.bindDataToView(view.bottom);
        selectTargetVM.showPopAllSelectFriends(view.bottom, LayoutPopSelectedFriendsBinding.inflate(getLayoutInflater()));
        selectTargetVM.submitTap(view.bottom.submit);
    }

    private class SelectAvatarAdapter extends RecyclerViewAdapter<MultipleChoice, RecyclerView.ViewHolder> {
        protected List<MultipleChoice> datas = new ArrayList<MultipleChoice>();
        public List<MultipleChoice> getDatas() {
            if (datas==null)
                datas = new ArrayList<MultipleChoice>();
            return datas;
        }
        public void setDatas(List<MultipleChoice> datas) {
            this.datas = datas;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyHolder(View.inflate(parent.getContext(), R.layout.item_group_select_avatar, null));
        }
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            AvatarImage imageView = ((MyHolder) holder).img;
            imageView.load(getDatas().get(position).icon,getDatas().get(position).name);
            imageView.setOnClickListener(new OnDedrepClickListener() {
                @Override
                public void click(View v) {
                }
            });
        }

        @Override
        public void onBindView(@NonNull RecyclerView.ViewHolder holder, MultipleChoice data, int position) {

        }

        @Override
        public int getItemCount() {
            return datas.size();
        }


        class MyHolder extends RecyclerView.ViewHolder {

            private AvatarImage img;

            public MyHolder(View itemView) {
                super(itemView);
                img = (AvatarImage) itemView.findViewById(R.id.headIv);
            }
        }
    }
}
