package com.casic.dcms.widgets;

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

import com.casic.dcms.R;
import com.casic.dcms.base.BaseApplication;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;

import java.util.Timer;
import java.util.TimerTask;

public class SignInDialog extends Dialog implements View.OnClickListener {

    private static final String TAG = "SignInDialog";
    private final String title;
    private final String address;
    private final OnDialogClickListener listener;
    private Timer timer;

    private SignInDialog(Builder builder) {
        super(builder.mContext);
        this.title = builder.title;
        this.address = builder.address;
        this.listener = builder.listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //手动设置dialog宽度
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.getDecorView().setBackgroundColor(Color.TRANSPARENT);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = QMUIDisplayHelper.dp2px(BaseApplication.getInstance(), 300);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

        setContentView(R.layout.dialog_sign_in);
        timer = new Timer();
        initView();
    }

    private void initView() {
        TextView clockView = findViewById(R.id.clockView);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                String systemTime = TimeOrDateUtil.minuteToTime(System.currentTimeMillis());
                clockView.post(() -> clockView.setText(systemTime));
            }
        }, 0, 1000);
        TextView optionTitleView = findViewById(R.id.optionTitleView);
        if (!TextUtils.isEmpty(title)) {
            optionTitleView.setText(title);
        }
        TextView signInPlaceView = findViewById(R.id.signInPlaceView);
        if (!TextUtils.isEmpty(address)) {
            signInPlaceView.setText(address);
        }
        TextView signInView = findViewById(R.id.signInView);
        signInView.setOnClickListener(this);
    }

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

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.signInView) {
            if (listener != null) {
                listener.onViewClick();
            }
        }
        this.dismiss();
    }

    public static class Builder {
        private Context mContext;
        private String title;
        private String address;
        private OnDialogClickListener listener;

        public Builder setContext(Context context) {
            this.mContext = context;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setAddress(String address) {
            this.address = address;
            return this;
        }

        public Builder setOnDialogClickListener(OnDialogClickListener listener) {
            this.listener = listener;
            return this;
        }

        public SignInDialog build() {
            return new SignInDialog(this);
        }
    }

    public interface OnDialogClickListener {
        void onViewClick();
    }
}