package com.wasu.cs.adapter;

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.ImageView;
import android.widget.TextView;

import com.wasu.cs.model.DemandRecommand;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;

import cn.com.wasu.main.R;

/**
 * Created by hudong on 16-6-30.
 */
public class RecommendAdapter extends BaseAdapter {
    private final int RECOMMEND_MAXNUM = 10;
    Context context;
    DemandRecommand demandRecommand;
    public RecommendAdapter(Context context, DemandRecommand demandRecommand){
        this.context=context;
        this.demandRecommand=demandRecommand;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        TextView textView;
        if (convertView == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_recommend, null);
        }
        imageView = (ImageView) convertView.findViewById(R.id.ivPic);
        textView = (TextView) convertView.findViewById(R.id.name);
        if (demandRecommand == null) {
            imageView.setImageResource(R.drawable.default_wasu_bg);
        } else {
            FrescoImageFetcherModule.getInstance().attachImage(
                    demandRecommand.getRecommendItems().get(position)
                            .getPicUrl(), imageView);
            textView.setText(demandRecommand.getRecommendItems()
                    .get(position).getTitle());
        }

        convertView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    return v.performClick();
                }
                return false;
            }
        });
        convertView.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    v.setScaleX(1.2f);
                    v.setScaleY(1.2f);
                } else {
                    v.setScaleX(1.0f);
                    v.setScaleY(1.0f);
                }
            }
        });
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getCount() {
        if (demandRecommand == null) {
            return 0;
        } else {
            return demandRecommand.getRecommendItems().size() > RECOMMEND_MAXNUM ? RECOMMEND_MAXNUM : demandRecommand.getRecommendItems().size();
        }
    }
}
