package com.blankj.androidutilcode.feature.core.network;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.blankj.androidutilcode.R;
import com.blankj.androidutilcode.base.BaseBackActivity;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.ThreadUtils;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/10/13
 *     desc  : demo about NetworkUtils
 * </pre>
 */
public class NetworkActivity extends BaseBackActivity {

    TextView tvAboutNetwork;
    TextView tvAboutNetworkAsync;
    ThreadUtils.SimpleTask mSimpleTask = new ThreadUtils.SimpleTask<String>() {
        @Override
        public String doInBackground() {
            return "isAvailableByPing: " + NetworkUtils.isAvailableByPing()
                    + "\ngetDomainAddress: " + NetworkUtils.getDomainAddress("baidu.com");
        }

        @Override
        public void onSuccess(String result) {
            tvAboutNetworkAsync.setText(result);
        }
    };

    public static void start(Context context) {
        Intent starter = new Intent(context, NetworkActivity.class);
        context.startActivity(starter);
    }

    @Override
    public void initData(@Nullable Bundle bundle) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_network;
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        getToolBar().setTitle(getString(R.string.demo_network));

        tvAboutNetwork = findViewById(R.id.tv_about_network);
        tvAboutNetworkAsync = findViewById(R.id.tv_about_network_async);
        findViewById(R.id.btn_open_wireless_settings).setOnClickListener(this);
        findViewById(R.id.btn_set_wifi_enabled).setOnClickListener(this);
        updateAboutNetwork();
    }

    @Override
    public void doBusiness() {
        ThreadUtils.executeBySingle(mSimpleTask);
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.btn_open_wireless_settings:
                NetworkUtils.openWirelessSettings();
                break;
            case R.id.btn_set_data_enabled:
                NetworkUtils.setMobileDataEnabled(!NetworkUtils.getMobileDataEnabled());
                break;
            case R.id.btn_set_wifi_enabled:
                NetworkUtils.setWifiEnabled(!NetworkUtils.getWifiEnabled());
                break;
        }
        updateAboutNetwork();
    }

    private void updateAboutNetwork() {
        tvAboutNetwork.setText(new SpanUtils()
                .appendLine("isConnected: " + NetworkUtils.isConnected())
                .appendLine("getMobileDataEnabled: " + NetworkUtils.getMobileDataEnabled())
                .appendLine("isMobileData: " + NetworkUtils.isMobileData())
                .appendLine("is4G: " + NetworkUtils.is4G())
                .appendLine("getWifiEnabled: " + NetworkUtils.getWifiEnabled())
                .appendLine("isWifiConnected: " + NetworkUtils.isWifiConnected())
                .appendLine("isWifiAvailable: " + NetworkUtils.isWifiAvailable())
                .appendLine("getNetworkOperatorName: " + NetworkUtils.getNetworkOperatorName())
                .appendLine("getNetworkTypeName: " + NetworkUtils.getNetworkType())
                .append("getIPAddress: " + NetworkUtils.getIPAddress(true))
                .create()
        );
    }

    @Override
    protected void onDestroy() {
        mSimpleTask.cancel();
        super.onDestroy();
    }
}
