package com.xplora.xpsettings.Activity;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.ParcelUuid;
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.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 = getResources().getStringArray(R.array.connections_title_array);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            mDataList.add(model);
        }
    }

    public void initValue() {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        boolean isBluetoothOn = bluetoothAdapter.isEnabled();

        WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        boolean isWifiOn = wifiManager.isWifiEnabled();

        boolean isRoamingOn = DataManager.getDataRoamingType() == 1;

        mDataList.get(0).isOn = isBluetoothOn;
        mDataList.get(1).isOn = isWifiOn;
        mDataList.get(2).isOn = isRoamingOn;
    }

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

        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;
                default:
                    break;
            }
        });
    }

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