package com.hongyegroup.cpt_rewards.widget;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.guadou.lib_baselib.utils.CommUtils;
import com.hongyegroup.cpt_rewards.R;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class PointsPaymentDialog extends DialogFragment {

    private ImageView mDeleteIv;
    private TextView mPayNowTv;
    private int mTotalPoint;

    private OnClickPointsPaymentListener mListener;

    public PointsPaymentDialog(int totalPoint) {

        mTotalPoint = totalPoint;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Objects.requireNonNull(getDialog().getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        View inflateView = inflater.inflate(R.layout.dialog_points_payment, null);

        return inflateView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mDeleteIv = view.findViewById(R.id.iv_dialog_points_payment_delete);
        mPayNowTv = view.findViewById(R.id.iv_dialog_points_payment_pay_now);
        TextView mDesTv = view.findViewById(R.id.tv_dialog_points_payment_des);

        String pointStr = String.valueOf(mTotalPoint);

        String splitStr="确定要花";
        String desStr = "确定要花" + pointStr + "积分兑换商品吗？";
        SpannableString spannableString = new SpannableString(desStr);
        spannableString.setSpan(new RelativeSizeSpan(1.2f), splitStr.length(), splitStr.length() + pointStr.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(CommUtils.getColor(R.color.profile_red)), splitStr.length(), splitStr.length() + pointStr.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mDesTv.setText(spannableString);

        initListener();

    }

    @SuppressLint("CheckResult")
    private void initListener() {

        //点击Delete

        mDeleteIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                dismiss();

            }
        });

        //点击Pay Now
        mPayNowTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mListener != null) {

                    mListener.OnClickPayNowListener();

                }

                dismiss();

            }
        });

    }

    public void setOnClickPointsPaymentDialogListener(OnClickPointsPaymentListener listener) {

        if (listener != null) {

            mListener = listener;
        }

    }

    public interface OnClickPointsPaymentListener {

        void OnClickPayNowListener();

    }

}
