package com.android.chmo.ui.adpater;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.BrokerInfo;
import com.android.chmo.ui.activity.model.BrokerModelActivity;
import com.android.chmo.ui.activity.model.ModelDetailActivity;
import com.android.chmo.utils.PixelUtils;
import com.android.chmo.utils.XUtilsImage;

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

public class BrokerAdapter extends CommonAdapter<BrokerInfo> {
    private BaseActivity baseActivity;
    private int imgWidth;
    private int imgHeight;


    public BrokerAdapter(BaseActivity activity) {
        super(activity);
        baseActivity = activity;
        imgWidth = PixelUtils.getWindowWidth() / 3-20;
        imgHeight = (int)Math.round(1.34 * imgWidth) ;
        try {
            Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.mipmap.def_img1);
            imgWidth = bitmap.getWidth();
            imgHeight = bitmap.getHeight();
        }catch (OutOfMemoryError e){
            System.runFinalization();
            System.gc();
        }
    }

    @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_broker_item, null);
            holder.headImage = (ImageView) convertView.findViewById(R.id.head);
            holder.nameView = (TextView) convertView.findViewById(R.id.name);
            holder.modelCountView = (TextView) convertView.findViewById(R.id.model_count);
            holder.brokerClick = convertView.findViewById(R.id.brokerClick);
            holder.imgContainer = (LinearLayout) convertView.findViewById(R.id.imageContainer);
            convertView.setTag(holder);
        }

        final BrokerInfo info = mList.get(position);
        holder.nameView.setText(info.name);
        holder.modelCountView.setText("模特" + info.modelcount + "人");
        XUtilsImage.display(holder.headImage, HttpApi.getImgUrl(info.logo),R.mipmap.def_img3);
        fillModelImages(holder, info);

        holder.brokerClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(baseActivity, BrokerModelActivity.class);
                intent.putExtra("broker", info);
                baseActivity.openPage(intent);
            }
        });

        return convertView;
    }

    private void fillModelImages(ViewHolder holder, BrokerInfo info) {
        holder.imgContainer.removeAllViews();
        if(info.modelphoto == null) {
            return;
        }
        for (int i=0; i < info.modelphoto.size(); i++) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(R.mipmap.def_img1);
            XUtilsImage.display(imageView, HttpApi.getImgUrl(info.modelphoto.get(i)), R.mipmap.def_img1);

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(imgWidth,imgHeight);
            lp.rightMargin = 10;
            holder.imgContainer.addView(imageView,lp);

            final String modelPk = info.photopk.get(i);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(baseActivity, ModelDetailActivity.class);
                    intent.putExtra("modelPk", modelPk);
                    baseActivity.openPage(intent);
                }
            });
        }

    }


    private class ViewHolder {
        ImageView headImage;
        TextView nameView;
        TextView modelCountView;
        View brokerClick;
        LinearLayout imgContainer;
    }
}
