package com.android.chmo.ui.adpater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.http.HttpApi;
import com.android.chmo.http.response.PromoteRes;
import com.android.chmo.model.Promote;
import com.android.chmo.utils.XUtilsImage;


import butterknife.BindView;
import butterknife.ButterKnife;

public class PromoteAdapter extends RecyclerView.Adapter<PromoteAdapter.PromoteHolder> {
    private PromoteRes data;
    private Context    context;

    public PromoteAdapter(PromoteRes data, Context context) {
        this.data = data;
        this.context = context;
    }

    public PromoteRes getData() {
        return data;
    }

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

    @Override
    public PromoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new PromoteHeaderHolder(LayoutInflater.from(context).inflate(R.layout.header_promote, parent, false));
        }
        return new PromoteItemHolder(LayoutInflater.from(context).inflate(R.layout.list_promote, parent, false));
    }

    @Override
    public void onBindViewHolder(PromoteHolder holder, int position) {
        if (getItemViewType(position) == 0) {
            ((PromoteHeaderHolder) holder).tvCoin.setText(data.getTotalCoin());
            ((PromoteHeaderHolder) holder).tvInviteNum.setText(data.getIntroduceNum());
        } else {
            Promote promote = data.getIntroduceList().get(position - 1);
            ((PromoteItemHolder) holder).tvCoin.setText(promote.getCoin());
            ((PromoteItemHolder) holder).tvName.setText(promote.getName());
            XUtilsImage.displayCircluar(((PromoteItemHolder) holder).ivAvatar, HttpApi.getImgUrl(promote.getAvatar()), R.mipmap.def_head);
        }
    }

    @Override
    public int getItemCount() {
        return 1 + data.getIntroduceList().size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        }
        return 1;
    }

    abstract class PromoteHolder extends RecyclerView.ViewHolder {
        PromoteHolder(View itemView) {
            super(itemView);
        }
    }

    class PromoteItemHolder extends PromoteHolder {
        @BindView(R.id.tv_coin)
        TextView  tvCoin;
        @BindView(R.id.iv_avatar)
        ImageView ivAvatar;
        @BindView(R.id.tv_name)
        TextView  tvName;

        PromoteItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class PromoteHeaderHolder extends PromoteHolder {
        @BindView(R.id.tv_invite_num)
        TextView tvInviteNum;
        @BindView(R.id.tv_coin)
        TextView tvCoin;

        PromoteHeaderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
