package com.android.chmo.ui.adpater;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
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;

import java.util.List;

/**
 * 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 = convertView.findViewById(R.id.head);
            holder.nameView = convertView.findViewById(R.id.name);
            holder.modelCountView = convertView.findViewById(R.id.model_count);
            holder.brokerClick = convertView.findViewById(R.id.brokerClick);
            holder.rvPhotos = convertView.findViewById(R.id.rv_photos);
            holder.rvPhotos.setLayoutManager(new LinearLayoutManager(baseActivity, LinearLayoutManager.HORIZONTAL, false));
            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);

        photoAdapter adapter = new photoAdapter(info);
        holder.rvPhotos.setAdapter(adapter);

        holder.brokerClick.setOnClickListener(v -> {
            Intent intent = new Intent(baseActivity, BrokerModelActivity.class);
            intent.putExtra("broker", info);
            baseActivity.openPage(intent);
        });

        return convertView;
    }

    private class ViewHolder {
        ImageView    headImage;
        TextView     nameView;
        TextView     modelCountView;
        View         brokerClick;
        RecyclerView rvPhotos;
    }

    class photoHolder extends RecyclerView.ViewHolder {

        public photoHolder(View itemView) {
            super(itemView);
        }
    }

    class photoAdapter extends RecyclerView.Adapter<photoHolder> {
        private BrokerInfo data;

        public void setData(BrokerInfo data) {
            this.data = data;
        }

        public photoAdapter(BrokerInfo data) {
            this.data = data;
        }

        @Override
        public photoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(imgWidth, imgHeight);
            params.rightMargin = 10;
            ImageView imageView = new ImageView(baseActivity);
            imageView.setLayoutParams(params);
            return new photoHolder(imageView);
        }

        @Override
        public void onBindViewHolder(photoHolder holder, int position) {
            ImageView imageView = (ImageView) holder.itemView;
            XUtilsImage.display(imageView, HttpApi.getImgUrl(data.modelphoto.get(position)), R.mipmap.def_img1);
            final String modelPk = data.photopk.get(position);
            imageView.setOnClickListener(v -> {
                Intent intent = new Intent(baseActivity, ModelDetailActivity.class);
                intent.putExtra("modelPk", modelPk);
                baseActivity.openPage(intent);
            });
        }

        @Override
        public int getItemCount() {
            if (data != null) {
                return data.modelphoto.size();
            }
            return 0;
        }
    }


}
