package com.maplehaze.adsdk.demo.utils;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.maplehaze.adsdk.base.MhCouponInfo;
import com.maplehaze.adsdk.demo.R;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class CouponUtil {
    private static final String TAG = "CouponUtil";

    public static FrameLayout.LayoutParams getFrameLayoutParams() {
        return new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
    }

    public static View getCouponView(Context context, MhCouponInfo info) {
        View couponView = LayoutInflater.from(context).inflate(R.layout.item_coupon_layout, null);
        TextView amountTv=couponView.findViewById(R.id.amount_tv);
        TextView titleTv=couponView.findViewById(R.id.title_tv);
        TextView validityTv=couponView.findViewById(R.id.tv_validity);
//        TextView actionTv=couponView.findViewById(R.id.tv_action);
        ImageView closeImg=couponView.findViewById(R.id.close_img);

        closeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (couponView.getParent()!=null){
                    ViewGroup parent=(ViewGroup) couponView.getParent();
                    parent.removeView(couponView);
                }
            }
        });

        if (info!=null){
            int couponType = info.getCouponType();//优惠券类型 1：满减优惠券
            int couponThreshold = info.getCouponThreshold();//满减 单位分
            int couponValue = info.getCouponValue();//优惠金额 单位分
            int couponTime = info.getCouponTime();//优惠券有效时间 单位分钟
            Log.i(TAG,"couponType="+couponType+" couponValue="+couponValue+" couponThreshold="+couponThreshold+" couponTime="+couponTime);


            amountTv.setText(context.getResources().getString(R.string.coupon_amount,convertFenToYuanString(couponValue)));
            titleTv.setText(context.getResources().getString(R.string.coupon_discount_amount,convertFenToYuanString(couponThreshold)));
            validityTv.setText(context.getResources().getString(R.string.coupon_discount_time,couponTime+""));
        }

        return couponView;
    }


    private static String convertFenToYuanString(int fen) {
        // 转换为 BigDecimal
        BigDecimal fenNumber = new BigDecimal(fen);
        BigDecimal divisor = new BigDecimal(100);
        BigDecimal yuanNumber = fenNumber.divide(divisor);

        // 非格式化：去掉末尾的零
        if (fen % 100 == 0) {
            // 整数，不需要小数部分
            return String.valueOf(yuanNumber.intValue());
        } else if (fen % 10 == 0) {
            // 只有一位小数
            DecimalFormat formatter = new DecimalFormat("0.0");
            return formatter.format(yuanNumber);
        } else {
            // 两位小数
            DecimalFormat formatter = new DecimalFormat("0.00");
            return formatter.format(yuanNumber);
        }
    }
}
