package com.tofly.ghrq_route.common.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.mgq.base.utils.customview.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.module.message.domain.InMessageBean;

import java.util.List;

/**
 * Created by dhc on 16/6/23.
 */
public class MessagePageAdapter extends PagerAdapter {
    private Context mContext;
    private View views[];
    private List<InMessageBean> mMessages;
    private ImageLoader imageLoader;

    public MessagePageAdapter(Context context, List<InMessageBean> messages){
        mContext = context;
        imageLoader = ImageLoader.getInstance();
        mMessages = messages;
        int pages = messages.size();
        views = new View[pages];
        for (int i = 0;  i < pages; i++){
            View rootView = View.inflate(context, R.layout.item_message_home_content, null);
            views[i] = rootView;
            InMessageBean messageBean = messages.get(i);
            if (messageBean != null){
                TextView textUser = rootView.findViewById(R.id.text_user);
                TextView textDate = rootView.findViewById(R.id.text_date);
                TextView textDesc = rootView.findViewById(R.id.text_desc);
                CircleImageView imageIcon = rootView.findViewById(R.id.image_user_icon);
                textUser.setText(messageBean.getCreateUserName());
                textDate.setText(messageBean.getCreateDate());
                textDesc.setText(messageBean.getMessage());
                String userAvatar = messageBean.getCreateUserAvatar();
                if (userAvatar !=null) {
                    String faceurl = NetRequestURL.getHttpHead() + messageBean.getCreateUserAvatar();
                    DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
                            .showImageOnFail(R.mipmap.ico_user_name).build();
                    imageLoader.displayImage(faceurl, imageIcon, imageOptions);
                }else {
                    imageIcon.setImageResource(R.mipmap.user_default_icon);
                }
            }
        }
    }

    public View getView(int postion){
        return views[postion];
    }

    @Override
    public int getCount() {
        return views.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView(views[position]);
    }

    public Object instantiateItem(View container, int position) {
        ((ViewPager) container).addView(views[position]);
        return views[position];
    }
}
