package com.xplora.xpsettings.Activity;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
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.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelUuid;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.xplora.xpsettings.Adapter.BaseListAdapter;
import com.xplora.xpsettings.Adapter.ConnectionsAdapter;
import com.xplora.xpsettings.Data.BtManager;
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 BaseListAdapter 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();
            //只显示 wifi
            model.cellType = i == 1 ? Constant.CellType.TITLE : Constant.CellType.GONE;
            mDataList.add(model);
        }
    }

    public void initValue() {
        if (Macros.DEBUG) {
            mDataList.get(0).isOn = false;
            mDataList.get(1).isOn = false;
            mDataList.get(1).subTitle = getString(R.string.data_roaming_off);
        } 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 = isWifiOn ? (isConnected ? (ssid == null ? "" : ssid): "") : getString(R.string.data_roaming_off);

            mDataList.get(1).isOn = isWifiOn;
            mDataList.get(1).subTitle = subTitle;
            Log.d(TAG, "initValue: Bluetooth:" + isBluetoothOn + " Wi-Fi:" + isWifiOn + " Name:" + 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 BaseListAdapter(ConnectionsActivity.this, 0, mDataList, mHandler);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setAdapter(adapter);
        listView.setDivider(null);
        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);
        Log.d(TAG, "onActivityResult: ");
        initValue();
        adapter.setBaseObjects(mDataList);
        adapter.notifyDataSetInvalidated();
    }

    Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == Constant.MSG_SWITCH_CHANGE) {
                int index = msg.arg1;
                int isOn = msg.arg2;
                BaseModel model = mDataList.get(index);
                model.isOn = isOn >= 1;
                BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (model.isOn) {
                    bluetoothAdapter.enable();
                } else {
                    bluetoothAdapter.disable();
                }
            }
        }
    };
}