package xn.hxp.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;

import java.io.File;

import xn.hxp.R;
import xn.hxp.databinding.DialogLoginVerifySuccessBinding;
import xn.hxp.http.HttpTool;
import xn.hxp.room.bean.BaseConfig;
import xn.hxp.room.bean.UserInfo;
import xn.hxp.room.tool.BaseConfigTool;
import xn.hxp.room.tool.UserInfoTool;
import xn.hxp.util.AudioPlayerTool;

public class LoginVerifySuccessDialog extends AppCompatDialog {
    private DialogLoginVerifySuccessBinding binding;
    private File faceFile;
    private CountDownTimer delayedDismissCDT;
    private int delayed = 3000;

    public LoginVerifySuccessDialog(@NonNull Context context, int delayed) {
        super(context, R.style.FullScreenDialogTheme);
        this.delayed = delayed * 1000;
        init();
    }

    public LoginVerifySuccessDialog(@NonNull Context context, int delayed, File faceFile) {
        super(context, R.style.FullScreenDialogTheme);
        this.faceFile = faceFile;
        this.delayed = delayed * 1000;
        init();
    }

    private void init() {
        AudioPlayerTool.getInstance().play(R.raw.login_verify_success);
        delayedDismissCDT = new CountDownTimer(delayed, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                int seconds = (int) Math.ceil(millisUntilFinished / 1000.0);
                binding.hint.setText(seconds + "秒后自动返回首页");
            }

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogLoginVerifySuccessBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        UserInfo userInfo = UserInfoTool.INSTANCE.getUserInfo();
        BaseConfig baseConfig = BaseConfigTool.INSTANCE.getBaseConfig();
        try {
            // 加载头像
            if (null == faceFile) {
                Glide.with(binding.userFace).load(HttpTool.INSTANCE.checkUrl(userInfo.getAvatar())).into(binding.userFace);
            }
            // 加载人脸
            else {
                LogUtils.d(faceFile);
                Glide.with(binding.userFace).load(faceFile).into(binding.userFace);
            }

            // 登录人名字
            binding.userName.setText(TextUtils.isEmpty(userInfo.getUserName()) ? "" : userInfo.getUserName());
            // 学校
            binding.schoolName.setText(TextUtils.isEmpty(baseConfig.getDeptName()) ? "" : baseConfig.getDeptName());
            // 地址
            String buildName = TextUtils.isEmpty(baseConfig.getBuildName()) ? "" : baseConfig.getBuildName();
            String floorName = TextUtils.isEmpty(baseConfig.getFloorName()) ? "" : baseConfig.getFloorName();
            binding.address.setText(buildName + floorName);
        } catch (Exception e) {
            LogUtils.e(e.getMessage());
        }
    }

    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        delayedDismissCDT.cancel();
    }
}
