package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.mdad.sdk.mdsdk.AdManager;
import com.mdad.sdk.mdsdk.common.AdData;
import com.sheep.gamegroup.model.entity.AdDataSon;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.List;
import java.util.Locale;
/**
 * Created by realicing on 2018/10/18.
 * realicing@sina.com
 * 幂动任务--普通任务和签到任务
 */
public class AdpMiDong extends AdbCommonRecycler<AdData> {
    private Context context;
    private boolean isSign = false;
    public AdpMiDong(Context context, List<AdData> list) {
        super(context, list);
        this.context = context;
    }

    public AdpMiDong setSign(boolean isSign){
        this.isSign = isSign;
        return this;
    }
    @Override
    public int getViewIdByType(int type) {
        return R.layout.adp_midong_item;
    }

    @Override
    public void convert(ViewHolder holder, final AdData data) {
        if(data == null)
            return;
        TextView btnInstall = holder.itemView.findViewById(R.id.task_type_tv);
        TextView tv_date = holder.itemView.findViewById(R.id.tv_date);
        RelativeLayout task_top = holder.itemView.findViewById(R.id.task_top);
        ViewUtil.setText((TextView) holder.itemView.findViewById(R.id.item_name_tv), data.getName());
        Glide.with(context).load(data.getLogo()).into((ImageView) holder.itemView.findViewById(R.id.item_icon_iv));
        try{
            ViewUtil.setText((TextView) holder.itemView.findViewById(R.id.item_money), TextUtils.isEmpty(data.getSize())?"0":data.getSize() +"M");
        }catch (Exception e){
            e.printStackTrace();
        }

        ViewUtil.setText(btnInstall, "安装");
        if (!isSign) {
            CommonUtil.getInstance().setDownloadTvText(context,btnInstall, data);
        } else {
            String todayTask = data.getDate();
            if (todayTask != null && todayTask.equals(TimeUtil.TimeStamp2Date(System.currentTimeMillis(), "yyyy-MM-dd"))) {
                CommonUtil.getInstance().setDownloadTvText(context,btnInstall, data);
                btnInstall.setEnabled(true);
                btnInstall.setBackground(context.getResources().getDrawable(R.drawable.sp_bg_gradient_rectangle_blue));
            } else {
                btnInstall.setEnabled(false);
                btnInstall.setText("时间还没到喔");
                btnInstall.setBackground(context.getResources().getDrawable(R.drawable.sp_rectangle_bg_gray_radius));
            }
        }

        if (!TextUtils.isEmpty(data.getTodayTask())) {
            ((TextView) holder.itemView.findViewById(R.id.item_date_tv)).setText(Html.fromHtml(String.format(Locale.CHINA, "签到可获得<font color='#ff4a5f'>%s元</font>", data.getUprice())));
            tv_date.setText(data.getTodayTask());
            if(TimeUtil.newInstance().big2Today(data.getTodayTask(), TimeUtil.FORMAT_DD_LINE)){
                task_top.setEnabled(true);
            }else {
                task_top.setEnabled(false);
            }
            task_top.setVisibility(View.VISIBLE);
        } else {//(data.getUprice() + data.getUsign_price_total())
            double price = data.getUprice() + data.getUsign_price_total();
            ((TextView) holder.itemView.findViewById(R.id.item_date_tv)).setText(Html.fromHtml(String.format(Locale.CHINA, "安装可获得<font color='#ff4a5f'>%s元</font>", NumberFormatUtils.retainMost2(price))));
            task_top.setVisibility(View.GONE);
        }

        btnInstall.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                /**
                 * 签到任务传1，非签到任务传0
                 */
                AdManager.getInstance(SheepApp.getInstance()).openOrDownLoadApps((Activity) context, data, isSign ? 1 : 0);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewUtil.newInstance().showMidongDialog((Activity) context, isSign, data, null);
            }
        });
    }

}
