package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xplora.xpsettings.Model.BluetoothModel;
import com.xplora.xpsettings.Model.WifiModel;
import com.xplora.xpsettings.R;


public class ConnectionsControlActivity extends BaseActivity {
    public int mViewType = 0; //0:bt  1:wifi
    public String btname = "";
    public String address = "";

    String ssid;
    String bssid;

    int status;

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

    private void initData() {
        Intent intent = getIntent();
        mViewType = intent.getIntExtra("view_type",  0);
        if (mViewType == 0) {
            btname = intent.getStringExtra("btname");
            address = intent.getStringExtra("address");
            status = intent.getIntExtra("status", BluetoothModel.STATUS.NA.ordinal());
        } else {
            ssid = intent.getStringExtra("ssid");
            bssid = intent.getStringExtra("bssid");
            status = intent.getIntExtra("status", WifiModel.STATUS.NA.ordinal());
        }
    }

    private void initView() {
        if (mViewType == 0) {
            initBluetoothView();
        } else {
            initWifiView();
        }
    }

    private void initBluetoothView() {
        TextView titleTextView = findViewById(R.id.title);
        Button btnDisconnect = findViewById(R.id.button0);
        Button btnForget = findViewById(R.id.button1);
        Button btnCancel = findViewById(R.id.button2);

        titleTextView.setText(btname);

        assert btnDisconnect != null;
        if(status == BluetoothModel.STATUS.CONNECTED.ordinal()) {
            btnDisconnect.setText(R.string.button_disconnected);
            btnDisconnect.setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.putExtra("address", address);
                intent.putExtra("operation", BluetoothActivity.OPERATE_DISCONNECT);
                setResult(RESULT_OK, intent);
                finish();
            });
        } else if(status == BluetoothModel.STATUS.PAIRED.ordinal()) {
            btnDisconnect.setText(R.string.button_connect);
            btnDisconnect.setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.putExtra("address", address);
                intent.putExtra("operation", BluetoothActivity.OPERATE_CONNECT);
                setResult(RESULT_OK, intent);
                finish();
            });
        }

        assert btnForget != null;
        btnForget.setText(R.string.button_cancel_paired);
        btnForget.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("address", address);
            intent.putExtra("operation", BluetoothActivity.OPERATE_UNPAIRED);
            setResult(RESULT_OK, intent);
            finish();
        });

        assert btnCancel != null;
        btnCancel.setText(R.string.button_cancel);
        btnCancel.setOnClickListener(v -> finish());
    }

    private void initWifiView() {
        TextView titleTextView = findViewById(R.id.title);
        Button btnDisconnect = findViewById(R.id.button0);
        Button btnForget = findViewById(R.id.button1);
        Button btnCancel = findViewById(R.id.button2);

        titleTextView.setText(ssid);

        assert btnDisconnect != null;
        if(status == WifiModel.STATUS.CONNECTED.ordinal()) {
            btnDisconnect.setText(R.string.button_disconnected);
            btnDisconnect.setVisibility(View.INVISIBLE);
            btnDisconnect.setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.putExtra("ssid", getIntent().getStringExtra("ssid"));
                intent.putExtra("bssid", getIntent().getStringExtra("bssid"));
                intent.putExtra("operation", WiFiActivity.OPERATE_DISCONNECT);
                setResult(-1, intent);
                finish();
            });
        } else if(status == WifiModel.STATUS.SAVED.ordinal()) {
            btnDisconnect.setText(R.string.button_connect);
            btnDisconnect.setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.putExtra("ssid", getIntent().getStringExtra("ssid"));
                intent.putExtra("bssid", getIntent().getStringExtra("bssid"));
                intent.putExtra("operation", WiFiActivity.OPERATE_CONNECT);
                setResult(-1, intent);
                finish();
            });
        }

        assert btnForget != null;
        btnForget.setText(R.string.button_forget);
        btnForget.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("ssid", getIntent().getStringExtra("ssid"));
            intent.putExtra("bssid", getIntent().getStringExtra("bssid"));
            intent.putExtra("operation", WiFiActivity.OPERATE_FORGET);
            setResult(-1, intent);
            finish();
        });

        assert btnCancel != null;
        btnCancel.setText(R.string.button_cancel);
        btnCancel.setOnClickListener(v -> finish());
    }
}