package com.sheep.gamegroup.view.adapter;

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

import com.sheep.gamegroup.model.entity.GameTaskOrderEnty;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;

import java.util.List;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.ORDER_MANAGER_CANCEL;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.ORDER_MANAGER_PAY;

/**
 * Created by ljy on 2018/6/15.
 */

public class AdpGameTaskOrderList extends BaseAdapter {
    private Context context;
    private List<GameTaskOrderEnty> etyList;

    public AdpGameTaskOrderList(Context context, List<GameTaskOrderEnty> etyList) {
        this.context = context;
        this.etyList = etyList;
    }
    @Override
    public int getCount() {
        return etyList.size();
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(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.adp_gametask_order_layout, null);
            holder.pay_type_tv = convertView.findViewById(R.id.pay_type_tv);
            holder.date_tv = convertView.findViewById(R.id.date_tv);
            holder.price_tv = convertView.findViewById(R.id.price_tv);
            holder.order_type_tv = convertView.findViewById(R.id.order_type_tv);
            holder.order_end_time_tv = convertView.findViewById(R.id.order_end_time_tv);
            holder.pay_cancel_tv = convertView.findViewById(R.id.pay_cancel_tv);
            holder.pay_tv = convertView.findViewById(R.id.pay_tv);
            holder.pay_tyoe_show_tv = convertView.findViewById(R.id.pay_tyoe_show_tv);
            holder.pay_layout = convertView.findViewById(R.id.pay_layout);
            holder.question_img = convertView.findViewById(R.id.question_img);
            holder.line_view = convertView.findViewById(R.id.line_view);
            convertView.setTag(holder);
        }
        final GameTaskOrderEnty gameTaskOrderEnty = getItem(position);

        if(gameTaskOrderEnty == null){
            return convertView;
        }
        String payType;
        if(gameTaskOrderEnty.getUser_amount() > 0){
            payType = "绵羊币(" + gameTaskOrderEnty.getUser_amount() +")";
        }else {
            payType = "";
        }
        if(gameTaskOrderEnty.getPay_type() == 1){
            if(!TextUtils.isEmpty(payType)){
                payType  += "\n" + " ";
            }else {
                payType  += "";
            }
            payType  += "支付宝(" + gameTaskOrderEnty.getThird_amount() + ")";
        }else {//3
            payType = "绵羊币(" + gameTaskOrderEnty.getAmount() +")";
        }
        holder.pay_type_tv.setText(payType);
        holder.date_tv.setText("提交时间："+TimeUtil.getDate(TimeUtil.FORMAT, Long.valueOf(gameTaskOrderEnty.getCreate_time()))+"");
        holder.price_tv.setText("提交金额："+gameTaskOrderEnty.getAmount());
        holder.order_type_tv.setText("订单状态：");
        holder.order_end_time_tv.setText("订单剩余时间："+(gameTaskOrderEnty.getCreate_time() == 0? 0:TimeUtil.getHours(gameTaskOrderEnty.getCreate_time()*1000))+"");
        holder.order_end_time_tv.setVisibility(View.GONE);

        holder.pay_layout.setVisibility(View.GONE);
        holder.pay_tyoe_show_tv.setVisibility(View.VISIBLE);
        holder.question_img.setVisibility(View.GONE);
        holder.line_view.setVisibility(View.VISIBLE);
        holder.pay_tyoe_show_tv.setTextColor(context.getResources().getColor(R.color.red_fd2d54));
        switch (gameTaskOrderEnty.getStatus()){
            case 0:
            default:
                holder.order_type_tv.setText("订单状态：未完成");
                holder.pay_layout.setVisibility(View.VISIBLE);
                holder.line_view.setVisibility(View.GONE);
                holder.pay_tyoe_show_tv.setVisibility(View.GONE);
                break;
            case 1:
                holder.order_type_tv.setText("订单状态：未完成");
                holder.pay_tyoe_show_tv.setText("放弃支付");
                break;
            case 2:
                holder.order_type_tv.setText("订单状态：未完成");
                holder.pay_tyoe_show_tv.setText("充值中");
                break;
            case 3:
                holder.order_type_tv.setText("订单状态：未完成");
                holder.pay_tyoe_show_tv.setText("充值中");
                break;
            case 4://type 1:游戏充值 2腾讯游戏  3小米游戏
                holder.order_type_tv.setText("订单状态：未完成");
                holder.pay_tyoe_show_tv.setText("充值失败");
                showQuestion(holder.question_img, gameTaskOrderEnty);
                break;
            case 5:
                holder.order_type_tv.setText("订单状态：已完成");
                holder.pay_tyoe_show_tv.setText("支付成功");
                holder.pay_tyoe_show_tv.setTextColor(context.getResources().getColor(R.color.green_btn_bg));
                showQuestion(holder.question_img, gameTaskOrderEnty);
                break;
            case 6://type 1:游戏充值 2腾讯游戏  3小米游戏
                holder.order_type_tv.setText("订单状态：订单失败");
                holder.pay_tyoe_show_tv.setText("已退款");
                showQuestion(holder.question_img, gameTaskOrderEnty);
                break;

        }

        holder.pay_cancel_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ORDER_MANAGER_CANCEL.onEvent();
                CommonUtil.getInstance()
                        .gameTaskPayOrGiveUp(
                                (Activity) context,
                                gameTaskOrderEnty.getPay_type(),
                                gameTaskOrderEnty.getId(),
                                2);
            }
        });
        holder.pay_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ORDER_MANAGER_PAY.onEvent();
                CommonUtil.getInstance()
                        .gameTaskPayOrGiveUp(
                                (Activity) context,
                                gameTaskOrderEnty.getPay_type(),
                                gameTaskOrderEnty.getId(),
                                1);
            }
        });
        return convertView;
    }

    class ViewHolder{
        LinearLayout pay_layout;
        TextView date_tv, price_tv, pay_type_tv,order_type_tv, order_end_time_tv, pay_tyoe_show_tv, pay_tv,pay_cancel_tv;
        ImageView question_img;
        View line_view;
    }

    /**
     * 是否显示？
     */
    private boolean showQuestion(View view, final GameTaskOrderEnty gameTaskOrderEnty ){

        if(!TextUtils.isEmpty(gameTaskOrderEnty.getRemark())){
            view.setVisibility(View.VISIBLE);

            //失败提示
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewUtil.showMsgDialog(context,
                            gameTaskOrderEnty.getRemark(),
                            gameTaskOrderEnty.isSuccess() ? "成功提示" : "失败原因",
                            false);
                }
            });
        }else {
            view.setVisibility(View.GONE);
        }

        return false;
    }
}
