package com.xplora.xpsettings.Activity;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.ParcelUuid;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.ListView;

import com.xplora.xpsettings.Adapter.ConnectionsAdapter;
import com.xplora.xpsettings.Data.DataManager;
import com.xplora.xpsettings.Model.BaseModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Constant;
import com.xplora.xpsettings.Utils.Macros;
import com.xplora.xpsettings.Utils.ResUtils;

import java.util.ArrayList;
import java.util.List;


public class ConnectionsActivity extends BaseActivity {
    public ConnectionsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        initData();
        initValue();
        initView();
    }

    private void initData() {
        String[] stringList = ResUtils.getStringArray("connections_title_array_", 4);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();

            //by lzx, 根据需求，去掉votle
            boolean isDisable = (i == 1 && DataManager.getIsDisableWifi()) || (i == 3);
            model.cellType = isDisable ? Constant.CellType.GONE : Constant.CellType.XPSTYLE;
            mDataList.add(model);
        }
    }

    public void initValue() {
        if (Macros.DEBUG) {
            mDataList.get(0).isOn = false;
            mDataList.get(1).isOn = false;
            mDataList.get(1).subTitle = "";
        } else {
            BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            boolean isBluetoothOn = bluetoothAdapter.isEnabled();
            mDataList.get(0).isOn = isBluetoothOn;

            WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            boolean isWifiOn = wifiManager.isWifiEnabled();
            WifiInfo info = wifiManager.getConnectionInfo();
            boolean isConnected = info.getSupplicantState() == SupplicantState.COMPLETED;
            String ssid = info.getSSID();
            String subTitle = "";
            if (ssid == null || ssid.trim().isEmpty() || ssid.contains("<unknown ssid>")) {
                ssid = info.getBSSID();
            } else {
                ssid = ssid.replace("\"", "");
            }
            subTitle = isConnected ? (ssid == null ? "" : ssid) : "";

            mDataList.get(1).isOn = isWifiOn;
            mDataList.get(1).subTitle = subTitle;
        }

        boolean isRoamingOn = DataManager.getDataRoamingType() == 1;
        if (mDataList.size() > 2) {
            mDataList.get(2).isOn = isRoamingOn;
            Log.d(TAG, "isRoamingOn: " + isRoamingOn);
        }

        int value = DataManager.getVoLTE();
        if (mDataList.size() > 3) {
            mDataList.get(3).isOn = value == 1;
        }
    }

    private void initView() {
        adapter = new ConnectionsAdapter(ConnectionsActivity.this, R.layout.item_xpstyle, mDataList);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setAdapter(adapter);
        listView.setDivider(new ColorDrawable(Color.TRANSPARENT));
        listView.setDividerHeight(0);

        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent;
            switch (i) {
                case 0:
                    intent = new Intent(ConnectionsActivity.this, BluetoothActivity.class);
                    startActivityForResult(intent, 0);
                    break;
                case 1:
                    intent = new Intent(ConnectionsActivity.this, WiFiActivity.class);
                    startActivityForResult(intent, 0);
                    break;
                case 2:
                    intent = new Intent(ConnectionsActivity.this, DataRoamingActivity.class);
                    startActivityForResult(intent, 0);
                    break;
                case 3:
                    intent = new Intent(ConnectionsActivity.this, VoLTEActivity.class);
                    startActivityForResult(intent, 0);
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initValue();
        adapter.setBaseObjects(mDataList);
        adapter.notifyDataSetInvalidated();
    }
}