package com.ihaoin.hooloo.device.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;

import com.ihaoin.hooloo.device.HLApplication;
import com.ihaoin.hooloo.device.R;
import com.ihaoin.hooloo.device.util.StringUtils;
import com.ihaoin.hooloo.device.util.Utils;

import java.util.Arrays;

public class TipsDialog extends Dialog {
    private View butnClose;
    private WebView webView;

    public TipsDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.view_tips);
        if (HLApplication.getMainData() == null || StringUtils.isEmpty(HLApplication.getMainData().getTips())) {
            dismiss();
            return;
        }
        initViews();
    }

    private void initViews() {
        webView = findViewById(R.id.web_view);
        butnClose = findViewById(R.id.butn_close);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return false;
            }
        });
        webView.loadUrl(HLApplication.getMainData().getTips());
        butnClose.setOnClickListener(x -> dismiss());

        Utils.setTouchDelegate(findViewById(R.id.layout_root), Arrays.asList(butnClose));
    }
}
