package com.sheep.gamegroup.view.dialog;

import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mdad.sdk.mdsdk.AdManager;
import com.mdad.sdk.mdsdk.common.AdData;
import com.sheep.gamegroup.model.entity.GiftBagApp;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.PackageUtil;

import org.afinal.simplecache.ACache;

import java.util.Locale;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.GIFT_BAG_DIALOG_COPY_CODE;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.GIFT_BAG_DIALOG_START_APP;

/**
 * Created by ljy on 2018/9/17.
 */

public class DialogMidong {
    private static Activity activity;

    public DialogMidong(Activity activity) {
        this.activity = activity;
    }

    public void showMidongDialog( final boolean isSign, final AdData data) {
        if(activity == null ||data == null)
            return;
        View dialog_parent = View.inflate(activity, R.layout.dialog_parent, null);
        final AlertDialog dialog = new AlertDialog.Builder(activity, R.style.MyDialogActivityTheme)
                .setView(dialog_parent)
                .create();
        TextView dialog_title = dialog_parent.findViewById(R.id.dialog_title);
        View dialog_close = dialog_parent.findViewById(R.id.dialog_close);
        LinearLayout dialog_center_ll = dialog_parent.findViewById(R.id.dialog_center_ll);
        dialog_center_ll.setPadding(0,0,0,0);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_midong_detail, dialog_center_ll, true);
        dialog_title.setVisibility(View.GONE);

        ImageView item_icon_iv = view.findViewById(R.id.item_icon_iv);
        TextView item_num_tv = view.findViewById(R.id.item_num_tv);
        TextView item_name_tv = view.findViewById(R.id.item_name_tv);
        TextView item_date_tv = view.findViewById(R.id.item_date_tv);
        TextView midong_remark_tv = view.findViewById(R.id.midong_remark_tv);
        TextView midong_btn_tv = view.findViewById(R.id.midong_btn_tv);

        ViewUtil.setText(item_name_tv, data.getName());

        String description = data.getDescription();
        if (TextUtils.isEmpty(description))
            description = data.getSign_description();
        if (TextUtils.isEmpty(description))
            description = "";
        ViewUtil.setText(midong_remark_tv, description);

        Glide.with(activity).load(data.getLogo()).into(item_icon_iv);


        if (!isSign) {
            CommonUtil.getInstance().setDownloadTvText(activity,midong_btn_tv, data);
         } else {
           String todayTask = data.getDate();
            if (todayTask != null && todayTask.equals(TimeUtil.newInstance().TimeStamp2Date(System.currentTimeMillis(), "yyyy-MM-dd"))) {
                CommonUtil.getInstance().setDownloadTvText(activity,midong_btn_tv, data);
                midong_btn_tv.setEnabled(true);
                midong_btn_tv.setTextColor(activity.getResources().getColor(R.color.blue_34a6e7));
            } else {
                midong_btn_tv.setEnabled(false);
                midong_btn_tv.setText("时间还没到喔");
                midong_btn_tv.setTextColor(activity.getResources().getColor(R.color.white));
            }
        }
        item_date_tv.setText((TextUtils.isEmpty(data.getSize())?"0":data.getSize() +"M"));
        if (!TextUtils.isEmpty(data.getTodayTask())) {
            item_num_tv.setText(Html.fromHtml(String.format(Locale.CHINA, "签到可获得<font color='#ff4a5f'>+%s元</font>", data.getUsign_price_total())));
        }else {
            item_num_tv.setText(Html.fromHtml(String.format(Locale.CHINA, "安装可获得<font color='#ff4a5f'>+%s元</font>,签到还可继续得<font color='#ff4a5f'>+%s元</font>", data.getUprice() , data.getUsign_price_total())));
        }

        midong_btn_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 签到任务传1，非签到任务传0
                 */
                AdManager.getInstance(SheepApp.getInstance()).openOrDownLoadApps(activity, data, isSign ? 1 : 0);

            }
        });
        dialog_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        try {
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
