package com.android.chmo.ui.adpater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.HttpApi;
import com.android.chmo.model.ActivitiesInfo;
import com.android.chmo.utils.PixelUtils;
import com.android.chmo.utils.XUtilsImage;

/**
 * Created by Administrator on 2018/8/1.
 */

public class ActivitiesAdapter extends CommonAdapter<ActivitiesInfo> {
    private BaseActivity baseActivity;
    private int imgWidth, imgHeight;

    public ActivitiesAdapter(BaseActivity activity) {
        super(activity);
        baseActivity = activity;
        imgWidth = PixelUtils.getWindowWidth();
        imgHeight =(int) (imgWidth / 1.4);
    }


    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(null != convertView) {
            holder = (ViewHolder) convertView.getTag();
        }
        if (null == holder) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.list_activities_item, null);
            holder.bigImage = (ImageView) convertView.findViewById(R.id.bigImg);
            holder.nameView = (TextView) convertView.findViewById(R.id.name);
            holder.timeView = (TextView) convertView.findViewById(R.id.time);
            holder.priceView = (TextView) convertView.findViewById(R.id.price);
            holder.signBtn = (TextView) convertView.findViewById(R.id.sign);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)holder.bigImage.getLayoutParams();
            if(null == lp) {
                lp = new LinearLayout.LayoutParams(imgWidth,imgHeight);
            }else {
                lp.width = imgWidth;
                lp.height = imgHeight;
            }
            holder.bigImage.setLayoutParams(lp);
            convertView.setTag(holder);
        }

        final ActivitiesInfo info = mList.get(position);
        XUtilsImage.display(holder.bigImage, HttpApi.getImgUrl(info.pic), R.mipmap.def_img2);

        holder.nameView.setText(info.caption);
        holder.priceView.setText("￥" + info.meprice);
        holder.signBtn.setText(info.signcount + "人已报名");

        if(info.issigndate == 1) {
            holder.timeView.setText(info.beg + "  开始");
            holder.priceView.setTextColor(baseActivity.getResources().getColor(R.color.pink));
            holder.signBtn.setBackgroundResource(R.mipmap.btn_sign);
        }else {
            holder.timeView.setText("已开始");
            holder.priceView.setTextColor(baseActivity.getResources().getColor(R.color.gray));
            holder.signBtn.setBackgroundResource(R.mipmap.btn_signed);
        }

        return convertView;
    }

    private class ViewHolder {
        ImageView bigImage;
        TextView nameView;
        TextView timeView;
        TextView priceView;
        TextView signBtn;
    }
}
