package com.xplora.xpsettings.Activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.xplora.xpsettings.R;

import java.util.Objects;


public class SoftwareVersionActivity extends BaseActivity {
    String mVersion;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.software_version_activity);

        //标题
        setWatchTitle(getResources().getStringArray(R.array.settings_title_array)[5]);

        initData();
        initView();
    }

    private void initData() {
        mVersion = Build.ID;
    }

    @SuppressLint("WifiManagerLeak")
    private int getWiFiStatus() {
        //wifi是否连接
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isConnected = mWifiInfo.isConnected();
        if (!isConnected)
            return 0;

        //wifi开关
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        boolean isOpen = wifiManager.isWifiEnabled();
        if (!isOpen)
            return 1;

        return -1;
    }

    private void initView() {
        TextView mHintText = findViewById(R.id.hintText);
        TextView mVersionText = findViewById(R.id.versionText);
        Button button1 = findViewById(R.id.button1);
        Button button2 = findViewById(R.id.button2);

        if (mHintText != null) {
            mHintText.setText(R.string.software_available);
        }
        if (mVersionText != null) {
            String s = getString(R.string.software_version);
            s += " " + mVersion;
            mVersionText.setText(s);
        }
        if (button1 != null)
            button1.setText(R.string.button_updatenow);
        if (button2 != null)
            button2.setText(R.string.button_cancel);

        Objects.requireNonNull(button1).setOnClickListener(v -> {
            int status = getWiFiStatus();
            Intent intent;
            if (status > -1) {
                intent = new Intent(SoftwareVersionActivity.this, GotoWiFiActivity.class);
                intent.putExtra("INTENT_WIFI_ERROR", status);
            }
            else {
                intent = new Intent(SoftwareVersionActivity.this, SoftwareUpdatingActivity.class);
            }
            startActivity(intent);
        });

        Objects.requireNonNull(button2).setOnClickListener(v -> finish());

    }
}