package com.tofly.ghrq_route.common.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.tofly.ghrq_route.R;


public class ConnectHintDialog {

    private Dialog mDialog;

    private TextView textConnectHint;
    private ImageView imageConnectHint;

    private Context mContext;


    public ConnectHintDialog(Context context) {
        mContext = context;
        initDialog();
    }

    public void initDialog() {
        mDialog = new Dialog(mContext, R.style.Theme_audioDialog);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.dialog_connect_hint, null);
        mDialog.setContentView(view);

        textConnectHint = view.findViewById(R.id.text_connect_hint);
        imageConnectHint = view.findViewById(R.id.image_connect_hint);

        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = -150; // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    public void showServerConnected(){
        if (mDialog != null) {
            textConnectHint.setText(R.string.server_connected);
            imageConnectHint.setImageResource(R.drawable.server_connect_white);

            if (!mDialog.isShowing())
                mDialog.show();

            dismissDialogDelayed();
        }
    }

    public void showServerDisconnected(){
        if (mDialog != null) {
            textConnectHint.setText(R.string.server_disconnected);
            imageConnectHint.setImageResource(R.drawable.server_disconnec_white);

            if (!mDialog.isShowing())
                mDialog.show();

            dismissDialogDelayed();
        }
    }

    public void showNetworkConnected(){
        if (mDialog != null) {
            textConnectHint.setText(R.string.network_connected);
            imageConnectHint.setImageResource(R.drawable.network_connect_white);

            if (!mDialog.isShowing())
                mDialog.show();

            dismissDialogDelayed();
        }
    }

    public void showNetworkDisconnected(){
        if (mDialog != null) {
            textConnectHint.setText(R.string.network_disconnected);
            imageConnectHint.setImageResource(R.drawable.network_disconnect_white);

            if (!mDialog.isShowing())
                mDialog.show();

            dismissDialogDelayed();
        }
    }

    private void dismissDialogDelayed(){
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mDialog != null && mDialog.isShowing()) {
                    mDialog.hide();
                }
                mDialog = null;
            }
        }, 2000);
    }

    // 隐藏dialog
    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }
}
