package com.android.chmo.ui.adpater;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
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.ModelInfo;
import com.android.chmo.model.ModelPhoto;
import com.android.chmo.ui.activity.model.ModelDetailActivity;
import com.android.chmo.ui.view.StarView;
import com.android.chmo.utils.PixelUtils;
import com.android.chmo.utils.XUtilsImage;

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

public class ModelAdapter extends CommonAdapter<ModelInfo> {
    private BaseActivity baseActivity;

    private int imgWidth;
    private int mType;



    public ModelAdapter(BaseActivity activity) {
        super(activity);
        baseActivity = activity;
        imgWidth = PixelUtils.getWindowWidth();
    }


    public void setType(int type) {
        mType = type;
    }



    @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_model_item, null);
            holder.bigImage = (ImageView) convertView.findViewById(R.id.bigImg);
            holder.statusImage = (ImageView) convertView.findViewById(R.id.status_img);
            holder.nameView = (TextView) convertView.findViewById(R.id.name);
            holder.starView = (StarView) convertView.findViewById(R.id.star);
            holder.labelsView = (TextView) convertView.findViewById(R.id.labels);
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)holder.bigImage.getLayoutParams();
            if(null == lp) {
                lp = new FrameLayout.LayoutParams(imgWidth,imgWidth);
            }else{
                lp.width = imgWidth;
                lp.height = imgWidth;
            }
            holder.bigImage.setLayoutParams(lp);
            convertView.setTag(holder);
        }

        final ModelInfo info = mList.get(position);

        holder.nameView.setText(info.name);
        holder.starView.setStar(info.star);
        if(info.modelphoto != null && info.modelphoto.size() > 0) {
            ModelPhoto photo = info.modelphoto.get(0);
            XUtilsImage.display(holder.bigImage, HttpApi.getImgUrl(photo.photo),R.mipmap.def_img2);
        }else {
            holder.bigImage.setImageResource(R.mipmap.def_img2);
        }
        if(mType == 2) {  // 推荐 显示个性签名
            holder.labelsView.setText(info.lname);
        } else {
            holder.labelsView.setText(info.skillnames);
        }

        if(info.is_a == 0) {
            holder.statusImage.setImageResource(R.mipmap.status_outline);
        }else if (info.is_a == 1) {
            holder.statusImage.setImageResource(R.mipmap.status_online);
        }else {
            holder.statusImage.setImageResource(R.mipmap.status_calling);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(baseActivity, ModelDetailActivity.class);
                intent.putExtra("model", info);
                baseActivity.openPage(intent);
            }
        });
        return convertView;
    }

    private class ViewHolder {
        ImageView bigImage;
        ImageView statusImage;
        TextView nameView;
        StarView starView;
        TextView labelsView;
    }
}
