package com.sheep.gamegroup.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.NewbieTask;
import com.sheep.jiuyan.samllsheep.R;

import java.util.List;
import java.util.Locale;

import static com.sheep.gamegroup.view.adapter.NewbieTaskAdp.doActionByTask;

/**
 * Created by realicing on 2018/5/9.
 */

public class DialogNewbieTaskAdp extends BaseAdapter {
    private Context context;
    private View.OnClickListener onClickListener;
    private List<NewbieTask> etyList;

    public DialogNewbieTaskAdp(Context context, List<NewbieTask> etyList) {
        this.context = context;
        this.etyList = etyList;
    }

    public DialogNewbieTaskAdp setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    @Override
    public int getCount() {
        return etyList.size();
    }

    @Override
    public Object getItem(int position) {
        return etyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Object tag = convertView == null ? null : convertView.getTag();
        ViewHolder holder;
        if(tag instanceof ViewHolder){
            holder = (ViewHolder) tag;
        }else {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.dialog_newbie_task_item, null);
            holder.dialog_sure_1 = convertView.findViewById(R.id.dialog_sure_1);
            convertView.setTag(holder);
        }

        final NewbieTask ety = (NewbieTask) getItem(position);
        setTextSure(holder.dialog_sure_1,ety, position);
        holder.dialog_sure_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onClickListener != null)
                    onClickListener.onClick(view);
                doActionByTask(context, ety);
            }
        });
        return convertView;
    }

    class ViewHolder{
        TextView dialog_sure_1;
    }


    private static void setTextSure(TextView dialog_sure, NewbieTask newbieTask, int index) {
        String tip = newbieTask.getName();
        switch (newbieTask.getId()){
            case 1://了解小绵羊奖励
                tip = "%d.点击了解小绵羊（+%.1f元）";
                break;
            case 2:
                tip = "%d.完成首次提现（+%.1f元）";
                break;
            case 3:
                tip = "%d.成功邀请好友注册（+%.1f元）";
                break;
            case 4:
                tip = "%d.首次做任务赚钱（额外奖+%.1f元）";
                break;
            case 6://加入官方QQ群
                tip = "%d.加入官方QQ群（+%.1f元）";
                break;
        }
        dialog_sure.setText(String.format(Locale.CHINA,tip,index+1, newbieTask.getAmount()));
    }
}
