package com.tofly.ghrq_route.common.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.AppCompatCheckedTextView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.user.domain.DepartUserBean;
import com.tofly.ghrq_route.module.user.domain.MessageUserBean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/5/15.
 */

public class MessageUserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public Context mContext;

    private List<DepartUserBean> mData;

    private OnItemClickListener mItemClickListener;
    private Set<Integer> selectedPostions = new HashSet<>();

    public MessageUserAdapter(Context context, List<DepartUserBean> userBeanList) {
        mContext = context;
        mData = userBeanList;
    }

    public void setData(List<DepartUserBean> userBeanList){
        mData = userBeanList;
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        boolean onItemLongClick(View view, int position);
    }

    public void setItemClickListener(OnItemClickListener listener){
        mItemClickListener = listener;
    }

    public void toggleSelection(int position){
        if (selectedPostions.contains(position)){
            selectedPostions.remove(position);
        }else {
            selectedPostions.add(position);
        }
        notifyItemChanged(position);
    }

    public void allSelected() {
        selectedPostions.clear();
        int index = 0;
        for (int i = 0; i < mData.size(); i++) {
            index++ ;
            List<MessageUserBean> users = mData.get(i).getUser();
            for (int j = 0; j < users.size(); j++,index++){
                selectedPostions.add(index);
            }
        }
        this.notifyDataSetChanged();
    }

    public void cancelSelected() {
        selectedPostions.clear();
        this.notifyDataSetChanged();
    }

    public int getSelectionSize(){
        return selectedPostions.size();
    }

    public List<MessageUserBean> getSelectedItems(){
        List<MessageUserBean> selectedList = new ArrayList<>();
        for (Integer postion : selectedPostions) {
            ItemStatus itemStatus = getItemStatusByPosition(postion);
            if (itemStatus.getViewType() == ItemStatus.VIEW_TYPE_SUBITEM){
                DepartUserBean departUser = mData.get(itemStatus.getGroupItemIndex());
                if (departUser != null){
                    MessageUserBean userBean = departUser.getUser().get(itemStatus.getSubItemIndex());
                    selectedList.add(userBean);
                }
            }
        }
        return selectedList;
    }

    private ItemStatus getItemStatusByPosition(int position) {
        ItemStatus itemStatus = new ItemStatus();

        int count = 0;
        int i = 0;

        for (i = 0; i < mData.size(); i++){
            if (count == position){
                itemStatus.setViewType(ItemStatus.VIEW_TYPE_GROUPITEM);
                itemStatus.setGroupItemIndex(i);
                break;
            } else if (count > position){
                itemStatus.setViewType(ItemStatus.VIEW_TYPE_SUBITEM);
                itemStatus.setGroupItemIndex(i - 1);
                itemStatus.setSubItemIndex(position - (count - mData.get(i - 1).getUser().size()));
                break;
            }

            /*header count*/
            count++;

            /*child count*/
            count += mData.get(i).getUser().size();
        }

        if ( i >= mData.size()){
            itemStatus.setGroupItemIndex(i - 1);
            itemStatus.setViewType(ItemStatus.VIEW_TYPE_SUBITEM);
            itemStatus.setSubItemIndex(position - (count - mData.get(i - 1).getUser().size()));
        }

        return itemStatus;
    }

    @Override
    public int getItemCount() {
        int itemCount = 0;

        if (mData.size() > 0){
            for (int i = 0; i < mData.size(); i++){
                itemCount += mData.get(i).getUser().size() + 1;
            }
        }
        return itemCount;
    }

    @Override
    public int getItemViewType(int position) {
        return getItemStatusByPosition(position).getViewType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == ItemStatus.VIEW_TYPE_GROUPITEM){
            View rootView = LayoutInflater.from(mContext).inflate(R.layout.listview_message_user_header_item, parent, false);
            viewHolder = new GroupItemViewHolder(rootView);
        }else {
            View rootView = LayoutInflater.from(mContext).inflate(R.layout.listview_message_user_item, parent, false);
            viewHolder = new SubItemViewHolder(rootView);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        final ItemStatus itemStatus = getItemStatusByPosition(position);
        DepartUserBean departBean = mData.get(itemStatus.getGroupItemIndex());
        if (itemStatus.getViewType() == ItemStatus.VIEW_TYPE_GROUPITEM){
            final GroupItemViewHolder groupHolder = (GroupItemViewHolder) viewHolder;
            if (departBean != null){
                groupHolder.textHeader.setText(departBean.getName());
            }
        }else if (itemStatus.getViewType() == ItemStatus.VIEW_TYPE_SUBITEM){
            final SubItemViewHolder subHolder = (SubItemViewHolder) viewHolder;
            if (departBean != null){
                int subIndex = itemStatus.getSubItemIndex();
                MessageUserBean userBean = departBean.getUser().get(subIndex);
                if (userBean != null){
                    subHolder.checkedTextUser.setText(userBean.getRealName());
                }
                if (selectedPostions.contains(position)){
                    subHolder.checkedTextUser.setChecked(true);
                }else {
                    subHolder.checkedTextUser.setChecked(false);
                }

                subHolder.checkedTextUser.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int adapterPostion = viewHolder.getAdapterPosition();
                        if (selectedPostions.contains(position)){
                            selectedPostions.remove(position);
                        }else {
                            selectedPostions.add(position);
                        }
                        notifyItemChanged(adapterPostion);
                        if (mItemClickListener != null){
                            mItemClickListener.onItemClick(view, adapterPostion);
                        }
                    }
                });
            }
        }
    }

    static class GroupItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text_header)
        AppCompatTextView textHeader;

        public GroupItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class SubItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.checktext_user)
        AppCompatCheckedTextView checkedTextUser;

        public SubItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    private static class ItemStatus {
        public static final int VIEW_TYPE_GROUPITEM = 0;
        public static final int VIEW_TYPE_SUBITEM = 1;

        private int viewType;
        private int groupItemIndex = 0;
        private int subItemIndex = -1;

        public ItemStatus() {

        }

        public int getViewType() {
            return viewType;
        }

        public void setViewType(int viewType) {
            this.viewType = viewType;
        }

        public int getGroupItemIndex() {
            return groupItemIndex;
        }

        public void setGroupItemIndex(int groupItemIndex) {
            this.groupItemIndex = groupItemIndex;
        }

        public int getSubItemIndex() {
            return subItemIndex;
        }

        public void setSubItemIndex(int subItemIndex) {
            this.subItemIndex = subItemIndex;
        }
    }
}
