package com.sheep.gamegroup.view.adapter;

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

import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.event.EventTypes;
import com.sheep.gamegroup.model.entity.NewbieTask;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;

import org.greenrobot.eventbus.EventBus;

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

import static com.sheep.gamegroup.util.UMConfigUtils.Event.NEWBIE_TASK_ITEM;

/**
 * Created by realicing on 2018/4/26.
 */

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

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

    public NewbieTaskAdp 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;
    }

    private final static int[] ids = {R.drawable.newbie_task_2, R.drawable.newbie_task_3, R.drawable.newbie_task_4, R.drawable.newbie_task_5, R.drawable.icon, R.drawable.newbie_task_6};

    @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.newbie_task, null);
            holder.newbie_task_iv = convertView.findViewById(R.id.newbie_task_iv);
            holder.newbie_task_name_tv = convertView.findViewById(R.id.newbie_task_name_tv);
            holder.newbie_task_money_tv = convertView.findViewById(R.id.newbie_task_money_tv);
            holder.newbie_task_tv = convertView.findViewById(R.id.newbie_task_tv);
            convertView.setTag(holder);
        }

        final NewbieTask ety = (NewbieTask) getItem(position);
        holder.newbie_task_iv.setImageResource(ids[(ety.getId() + ids.length - 1) % ids.length]);
        holder.newbie_task_name_tv.setText(ety.getName());
        holder.newbie_task_money_tv.setText(String.format(Locale.CHINESE, "+%.1f元", ety.getAmount()));
        holder.newbie_task_tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (onClickListener != null)
                    onClickListener.onClick(view);
                doActionByTask(context, ety);
            }
        });
        switch (ety.getStatus()) {
            case 0:
                holder.newbie_task_tv.setText("去完成");
                break;
            case 1:
                holder.newbie_task_tv.setText("审核中");
                break;
            case 2:
                holder.newbie_task_tv.setText("已完成");
                break;
            case 3:
                holder.newbie_task_tv.setText("去完成");
                break;
        }
        holder.newbie_task_tv.setEnabled(ety.getEnabled());
        return convertView;
    }

    class ViewHolder {
        ImageView newbie_task_iv;
        TextView newbie_task_name_tv, newbie_task_money_tv, newbie_task_tv;
    }

    public static void doActionByTask(Context context, NewbieTask task) {
        if (context instanceof Activity) {
            NEWBIE_TASK_ITEM.onEvent("id", task.getId(), "name", task.getName());
            switch (task.getId()) {
                case 1://了解小绵羊
                    Jump2View.getInstance().goUnderstandSheep(context, null);
                    break;
                case 2://完成首次提现
                    Jump2View.getInstance().tryGoWithdrawal(context, null);
                    break;
                case 3://成功邀请好友
                    Jump2View.getInstance().goAskGetMoney(context, null);
                    break;
                case 4://首次完成任务
                    Jump2View.getInstance().goTryplayView(context, null);
                    break;
                case 5://注册账号并登录
                    if (BuildConfig.DEBUG) {
                        G.showToast("测试：服务器返回数据错误，这个任务可能没有完成？");
                    }
                    if(TestUtil.isLTVersion3_4_5())//3.4.5以前版本才调用下面的方法
                        isShow(context);
                    break;
                case 6://加入官方QQ群
                    doActionByTaskQQ(context, task);
                    break;
            }
        } else {
            if (BuildConfig.DEBUG)
                G.showToast("测试：context必须继续Activity");
        }
    }

    private static void isShow(Context context) {
        UserEntity userEntity = DataUtil.getInstance().getUserEntity();
        if (((userEntity.getNewbie_task_status() & 16) == 0) && userEntity.getIs_new() == 1) {
            EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.FGT_SHEEP_SHOW_NEW_USER_HONG_BAO));
//            LogUtil.logI("sendmessage");
        }
    }

    private static void doActionByTaskQQ(Context context, NewbieTask task) {

        switch (task.getStatus()) {
            case 0://"去完成");
                Jump2View.getInstance().goAddQQTaskDetail(context, task);
                break;
            case 1://"审核中");
                break;
            case 2://"已完成");
                break;
            case 3://"去完成");
                Jump2View.getInstance().goAddQQTaskDetail(context, task);
                break;
        }
    }

}
