package xn.hxp.weidith;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.rc.httpcore.bean.UseNoBean;

import xn.hxp.databinding.PromptDialogBinding;

public class PromptDialog extends Dialog {
    private PromptDialogBinding binding;
    private UseNoBean useNoBean;
    private CountDownTimer countDownTimer;

    public PromptDialog(@NonNull Context context, UseNoBean useNoBean) {
        super(context);
        this.useNoBean = useNoBean;
        Window window = getWindow();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (null != window) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.CENTER);
            window.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
            );
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = PromptDialogBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        String chemicalName = useNoBean.getChemicalName();
        binding.chemicalName.setText(TextUtils.isEmpty(chemicalName) ? "无" : chemicalName);

        String casNum = useNoBean.getCasNum();
        binding.casNum.setText(TextUtils.isEmpty(casNum) ? "CAS:无" : "CAS:" + casNum);

        String chemicalCategory = useNoBean.getChemicalCategory();
        binding.chemicalCategory.setText(TextUtils.isEmpty(chemicalCategory) ? "类别:无" : "类别:" + chemicalCategory);

        String specUnit = useNoBean.getSpecUnit();
        String packUnit = useNoBean.getPackUnit();
        int specNum = useNoBean.getSpecNum();
        binding.sepl.setText("规格:" + specNum + (TextUtils.isEmpty(specUnit) ? "" : specUnit + "/" + (TextUtils.isEmpty(packUnit) ? "" : packUnit)));

        String expireTime = useNoBean.getExpireTime();
        Integer expireStatus = useNoBean.getExpireStatus();
        if (!TextUtils.isEmpty(expireTime) && null != expireStatus) {
            switch (expireStatus) {
                case 1:
                    binding.expireTime.setText("过期时间: " + expireTime);
                    binding.expire.setText("已过期");
                    break;
                case 2:
                    binding.expireTime.setText("过期时间: " + expireTime);
                    binding.expire.setText("即将过期");
                    break;
                default:
                    binding.expireTime.setText("过期时间: " + expireTime);
                    break;
            }
        } else {
            binding.expireTime.setText("过期时间:");
        }

        String tagCode = useNoBean.getTagCode();
        binding.tagCode.setText(TextUtils.isEmpty(tagCode) ? "编码:无" : "编码:" + tagCode);

        String chemicalLevelName = useNoBean.getChemicalLevelName();
        binding.chemicalLevelName.setText(TextUtils.isEmpty(chemicalLevelName) ? "级别:无" : "级别:" + chemicalLevelName);

        String surplus = useNoBean.getSurplus();
        binding.surplus.setText(TextUtils.isEmpty(surplus) ? "库存量:无" : "库存量:" + surplus);

        String belongName = useNoBean.getBelongName();
        binding.belongName.setText(TextUtils.isEmpty(belongName) ? "归属人:无" : "归属人:" + belongName);

        binding.butClos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        countDownTimer = new CountDownTimer(15 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (isShowing()) {
                    binding.butClos.setText("关闭(" + millisUntilFinished / 1000 + ")");
                }
            }

            @Override
            public void onFinish() {
                if (isShowing()) {
                    dismiss();
                }
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }
}
