package com.hh.arome.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hh.arome.R;
import com.hh.arome.common.Constants;
import com.hh.arome.utlis.device.DeviceUtils;

import java.util.List;

public class Gridadapter extends BaseAdapter {
    private List<String > list;
    private Context context;
    private OnItemClickListener listener;

    public Gridadapter(List<String> list, Context context) {//适配器
        super();
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (null != list) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService((Context.LAYOUT_INFLATER_SERVICE));
            if (Constants.DeviceModel.D2.equals(DeviceUtils.getDeviceModel())){
                convertView = layoutInflater.inflate(R.layout.enter_grid_adapter_hp, null);
            }else if (Constants.DeviceModel.K8.equals(DeviceUtils.getDeviceModel())) {
                convertView = layoutInflater.inflate(R.layout.enter_grid_adapter_sp, null);
            }

            viewHolder = new ViewHolder();
            viewHolder.gridadapter_Text = (TextView) convertView.findViewById(R.id.gridadapter_Text);
            viewHolder.gridadapter_Image = (LinearLayout) convertView.findViewById(R.id.gridadapter_Image);
            viewHolder.gridadapter_LinearLayout= (LinearLayout) convertView.findViewById(R.id.gridadapter_LinearLayout);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.gridadapter_Text.setText(list.get(position)+"");
        if (position==list.size()-1){
            viewHolder.gridadapter_Text.setVisibility(View.GONE);
            viewHolder.gridadapter_Image.setVisibility(View.VISIBLE);
        }else {
            viewHolder.gridadapter_Text.setVisibility(View.VISIBLE);
            viewHolder.gridadapter_Image.setVisibility(View.GONE);
        }
        viewHolder.gridadapter_LinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onItemClick(list.get(position));
            }
        });
        viewHolder.gridadapter_LinearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){//按下
                    viewHolder.gridadapter_LinearLayout.setBackgroundResource(R.drawable.rounded_10_light_grey_fill);
                }else if(event.getAction() == MotionEvent.ACTION_UP){//松开
                    viewHolder.gridadapter_LinearLayout.setBackgroundResource(R.drawable.rounded_10_white_fill);
                }
                return false;
            }
        });

        return convertView;
    }

    class ViewHolder {
        private TextView gridadapter_Text;
        private LinearLayout gridadapter_Image;
        private LinearLayout gridadapter_LinearLayout;
    }

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

    public interface OnItemClickListener {
        void onItemClick(String position);
    }
}