package com.handset.serialportsensor.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handset.serialportsensor.R;
import com.handset.serialportsensor.bean.SettingsItem;
import com.handset.serialportsensor.constant.Constant;

import java.util.List;

public class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.ViewHolder> {
    private List<SettingsItem> list;
    private int role;
    private OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.listener = onItemClickListener;
    }

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

    public SettingsAdapter(List<SettingsItem> list, int role) {
        this.list = list;
        this.role = role;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.settings_adapter_item, viewGroup, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        final SettingsItem item = list.get(i);
        viewHolder.iv_img.setImageResource(item.getIconRes());
        viewHolder.tv_title.setText(item.getName());
        viewHolder.itemView.setOnClickListener((View view) ->
                Toast.makeText(view.getContext(), item.getName(), Toast.LENGTH_SHORT).show()
        );
        if (i == list.size() - 1) {
            viewHolder.line.setVisibility(View.GONE);
            if (role == Constant.SENIOR_ROLE) {
                viewHolder.third_layout.setVisibility(View.VISIBLE);
            } else {
                viewHolder.third_layout.setVisibility(View.INVISIBLE);
            }
        } else {
            viewHolder.third_layout.setVisibility(View.GONE);
            viewHolder.line.setVisibility(View.VISIBLE);
        }

        viewHolder.itemView.setOnClickListener((View view) -> listener.onItemClick(view, i));
        if (role == Constant.NOT_CONNECTED_ROLE) {
            if (item.getPosition() == 0 || item.getPosition() == 2 || item.getPosition() == 7 || item.getPosition() == 9) {
                viewHolder.iv_img.setImageResource(R.mipmap.ic_settings3_gray);
                viewHolder.tv_title.setTextColor(viewHolder.tv_title.getContext().getResources().getColor(R.color.very_light_gray));
                viewHolder.iv_right.setImageResource(R.mipmap.ic_right_arrow_light);
                viewHolder.itemView.setClickable(false);
            }
        }
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_img;
        private TextView tv_title;
        private ImageView iv_right;
        private View line;
        private LinearLayout third_layout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_title = itemView.findViewById(R.id.tv_title);
            iv_right = itemView.findViewById(R.id.iv_right);
            line = itemView.findViewById(R.id.line);
            third_layout = itemView.findViewById(R.id.third_layout);
        }
    }
}


