package kr.co.icontrols.iotplatform.DragListView;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import kr.co.icontrols.iotplatform.DataClass.DeviceGroupInfo;
import kr.co.icontrols.iotplatform.DataClass.DeviceInfo;
import kr.co.icontrols.iotplatform.R;

import static kr.co.icontrols.iotplatform.utils.CommonUtils.getCircleBitmap;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.getGroupImageResource;
import static kr.co.icontrols.iotplatform.utils.Constants.DEFUALT_GROUP_INDEX;

/**
 * 그룹 순서 리스트 Adapter
 */
public class RankListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements
        SwipeAndDragHelper.ActionCompletionContract {

    public static final int MODE_GROUP = 0;
    public static final int MODE_DEVICE = 1;

    private static int mMode = 0;

    private static final int GROUP_TYPE = 1;
    private List<DeviceGroupInfo> itemsGroupList;
    private List<DeviceInfo> itemsDeviceList;
    private ItemTouchHelper touchHelper;
    private ChangeListener listener;

    private Activity activity;

    public RankListAdapter(ChangeListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        activity = (Activity)parent.getContext();
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listview_group_rank_list_item, parent, false);
        return new ItemViewHolder(view);
    }

    @Override

    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        switch (mMode) {
            case MODE_GROUP:
                if (itemViewType == GROUP_TYPE) {
                    ((ItemViewHolder) holder).txtGroupName.setText(itemsGroupList.get(position).friendlyName);
                    ((ItemViewHolder) holder).txtDeviceCount.setText(itemsGroupList.get(position).deviceCount + "개 스마트 조명");
                    ((ItemViewHolder) holder).imgIcon.setImageBitmap(getCircleBitmap(getGroupImageResource(itemsGroupList.get(position).icon)));
                    if (itemsGroupList.get(position).index == DEFUALT_GROUP_INDEX) {
                        ((ItemViewHolder) holder).imgDrag.setVisibility(View.INVISIBLE);
                    } else {
                        ((ItemViewHolder) holder).imgDrag.setOnTouchListener(new View.OnTouchListener() {
                            @Override
                            public boolean onTouch(View v, MotionEvent event) {
                                if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                                    touchHelper.startDrag(holder);
                                }
                                return false;
                            }
                        });
                    }
                }
                break;

            case MODE_DEVICE:
                ((ItemViewHolder) holder).txtGroupName.setText(itemsDeviceList.get(position).friendlyName.isEmpty()?itemsDeviceList.get(position).sharedFriendlyName.isEmpty()?activity.getString(R.string.no_name):itemsDeviceList.get(position).sharedFriendlyName:itemsDeviceList.get(position).friendlyName);
                ((ItemViewHolder) holder).txtDeviceCount.setText(itemsDeviceList.get(position).dgroupName);
                ((ItemViewHolder) holder).imgIcon.setImageResource(R.drawable.icon_add_device_light);
                ((ItemViewHolder) holder).imgIcon.setBackgroundResource(R.drawable.background_circle_add_device_icon);
                ((ItemViewHolder) holder).imgDrag.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                            touchHelper.startDrag(holder);
                        }
                        return false;
                    }
                });
                break;

        }
    }

    @Override
    public int getItemCount() {
        if(mMode == MODE_GROUP) {
            return itemsGroupList == null ? 0 : itemsGroupList.size();
        } else if(mMode == MODE_DEVICE) {
            return itemsDeviceList == null ? 0 : itemsDeviceList.size();
        } else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return GROUP_TYPE;
    }

    public void setIGroupInfoList(List<DeviceGroupInfo> itemsList) {
        mMode = MODE_GROUP;
        this.itemsGroupList = itemsList;
    }

    public void setDeviceInfoList(List<DeviceInfo> itemsList) {
        mMode = MODE_DEVICE;
        this.itemsDeviceList = itemsList;
    }

    @Override
    public void onViewMoved(int oldPosition, int newPosition) {

        if(mMode == MODE_GROUP) {
            DeviceGroupInfo targetUser = itemsGroupList.get(oldPosition);
            DeviceGroupInfo user = new DeviceGroupInfo(targetUser);
            itemsGroupList.remove(oldPosition);
            itemsGroupList.add(newPosition, user);

        } else if(mMode == MODE_DEVICE) {
            DeviceInfo targetUser = itemsDeviceList.get(oldPosition);
            DeviceInfo user = new DeviceInfo(targetUser);
            itemsDeviceList.remove(oldPosition);
            itemsDeviceList.add(newPosition, user);
        }
        notifyItemMoved(oldPosition, newPosition);

        if(listener != null) {
            listener.onChangeData();
        }
    }

    @Override
    public void onViewSwiped(int position) {
        if(mMode == MODE_GROUP) {
            itemsGroupList.remove(position);
        } else if(mMode == MODE_DEVICE) {
            itemsDeviceList.remove(position);
        }
        notifyItemRemoved(position);

        if(listener != null) {
            listener.onChangeData();
        }
    }

    public void setTouchHelper(ItemTouchHelper touchHelper) {
        this.touchHelper = touchHelper;
    }
}

