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.FindInfo;
import com.android.chmo.utils.DateUtils;
import com.android.chmo.utils.PixelUtils;
import com.android.chmo.utils.TimeUtils;
import com.android.chmo.utils.XUtilsImage;

import java.util.Date;

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

public class FindAdapter extends CommonAdapter<FindInfo>{
    private BaseActivity baseActivity;

    private int imgWidth;

    public FindAdapter(BaseActivity activity) {
        super(activity);
        baseActivity = activity;
        imgWidth = PixelUtils.getWindowWidth() - 20;
    }

    @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_find_item, null);
            holder.headView = (ImageView) convertView.findViewById(R.id.head);
            holder.bigImage = (ImageView) convertView.findViewById(R.id.bigImg);
            holder.nameView = (TextView) convertView.findViewById(R.id.name);
            holder.addrView = (TextView) convertView.findViewById(R.id.address);
            holder.timeView = (TextView) convertView.findViewById(R.id.time);
            holder.goodNum = (TextView) convertView.findViewById(R.id.goodText);
            holder.commentNum = (TextView) convertView.findViewById(R.id.commentText);
            holder.followText = (TextView) convertView.findViewById(R.id.followText);
            holder.goodIcon = (ImageView) convertView.findViewById(R.id.goodIcon);
            holder.followIcon = (ImageView) convertView.findViewById(R.id.followIcon);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)holder.bigImage.getLayoutParams();
            if(null == lp) {
                lp = new LinearLayout.LayoutParams(imgWidth,imgWidth);
            }else {
                lp.width = imgWidth;
                lp.height = imgWidth;
            }
            holder.bigImage.setLayoutParams(lp);
            convertView.setTag(holder);
        }

        final FindInfo info = mList.get(position);
        holder.nameView.setText(info.pet);
        holder.addrView.setText(info.area);
        holder.timeView.setText(getTimeStr(info.pdate));
        holder.goodNum.setText("点赞" + info.likescount);
        holder.commentNum.setText("评论" + info.commentcount);

        holder.goodIcon.setImageResource(info.islikes == 1 ? R.mipmap.gooded : R.mipmap.good);
        if(info.isattention == 1) {
            holder.followIcon.setImageResource(R.mipmap.liked);
            holder.followText.setTextColor(mContext.getResources().getColor(R.color.pink));
            holder.followText.setText("已关注");
        }else {
            holder.followIcon.setImageResource(R.mipmap.like);
            holder.followText.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.followText.setText("关注");
        }
        XUtilsImage.display(holder.bigImage, HttpApi.getImgUrl(info.img),R.mipmap.def_img2);
        XUtilsImage.displayCircluar(holder.headView, HttpApi.getImgUrl(info.hphoto), R.mipmap.def_head);

        return convertView;
    }

    private String getTimeStr(String time) {
        Date date = DateUtils.parseDate(time);
        return TimeUtils.friendlyTime(date.getTime()/1000);
    }


    private class ViewHolder {
        ImageView headView;
        ImageView bigImage;
        TextView nameView;
        TextView addrView;
        TextView timeView;
        TextView goodNum;
        ImageView goodIcon;
        TextView commentNum;
        TextView followText;
        ImageView followIcon;
    }
}
