package com.casic.dcms.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.casic.dcms.R;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.pengxh.androidx.lite.kit.DialogKit;

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);
        DialogKit.resetParams(this, 0.85);
        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);
        }
        Button signInButton = findViewById(R.id.signInButton);
        signInButton.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.signInButton) {
            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();
    }
}