/*
 * Android Scan, an app for scanning and configuring HBM devices.
 *
 * The MIT License (MIT)
 *
 * Copyright (C) Stephan Gatzka
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.hbm.devices.scan.ui.android;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.hbm.devices.scan.announce.Announce;
import com.hbm.devices.scan.announce.DefaultGateway;
import com.hbm.devices.scan.announce.Device;
import com.hbm.devices.scan.announce.IPv4Entry;

import java.util.List;

public final class ConfigureActivity extends AppCompatActivity {

    private Announce announce;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.configure_layout);

        announce = (Announce) getIntent().getSerializableExtra(DeviceDetailsActivity.DETAILS);
    	initToolbar(announce);

        setCurrentIp(announce);
        setCurrentGateway(announce);
    }

    private void setCurrentIp(Announce announce) {
        List<IPv4Entry> ipv4Addresses = announce.getParams().getNetSettings().getInterface().getIPv4();
        if (!ipv4Addresses.isEmpty()) {
            IPv4Entry entry = ipv4Addresses.get(0);
            TextView currentIp = (TextView) findViewById(R.id.configure_current_ip);
            currentIp.setText(entry.getAddress());
            TextView currentNetMask = (TextView) findViewById(R.id.configure_current_subnetmask);
            currentNetMask.setText(entry.getNetmask());
        }
    }

    private void setCurrentGateway(Announce announce) {
        DefaultGateway gateway = announce.getParams().getNetSettings().getDefaultGateway();
        if (gateway != null) {
            String ipv4Gateway = gateway.getIpv4Address();
            if (ipv4Gateway != null) {
                TextView currentGateway = (TextView) findViewById(R.id.configure_current_gateway);
                currentGateway.setText(ipv4Gateway);
            }
        }
    }

    private void initToolbar(Announce announce) {
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        setTitle(getDisplayName(announce.getParams().getDevice()));
    }

   	private String getDisplayName(Device device) {
        StringBuilder title = new StringBuilder(getResources().getText(R.string.configure));
        String displayName = device.getName();
        if (displayName == null || displayName.length() == 0) {
            displayName = device.getUuid();
        }
        title.append(" ");
        title.append(displayName);
        return title.toString();
    }
}

@@ -1253,16 +1253,48 @@
 droid;%0A%0A
+import android.content.Context;%0A
 import a
@@ -1311,16 +1311,16 @@
 Bundle;%0A
-
 import a
@@ -1439,24 +1439,211 @@
 et.Toolbar;%0A
+import android.view.WindowManager;%0Aimport android.view.inputmethod.InputMethodManager;%0Aimport android.widget.CompoundButton;%0Aimport android.widget.EditText;%0Aimport android.widget.Switch;%0A
 import andro
@@ -2295,24 +2295,24 @@
 (announce);%0A
-
         setC
@@ -2332,24 +2332,1133 @@
 (announce);%0A
+        setEdit(false);%0A%0A        Switch dhcpSwitch = (Switch) findViewById(R.id.dhcp_switch);%0A        dhcpSwitch.setChecked(true);%0A        dhcpSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() %7B%0A            @Override%0A            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) %7B%0A                if (isChecked) %7B%0A                    setEdit(false);%0A                %7D else %7B%0A                    setEdit(true);%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void setEdit(boolean edit) %7B%0A        EditText ipv4 = (EditText) findViewById(R.id.configure_ip_address_edit);%0A        ipv4.setEnabled(edit);%0A        if (edit) %7B%0A            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);%0A            imm.showSoftInput(ipv4, InputMethodManager.SHOW_IMPLICIT);%0A        %7D%0A        EditText ipv4Mask = (EditText) findViewById(R.id.configure_subnet_edit);%0A        ipv4Mask.setEnabled(edit);%0A        EditText gateway = (EditText) findViewById(R.id.configure_gateway_ip_edit);%0A        gateway.setEnabled(edit);%0A
     %7D%0A%0A    p

Disable edit text fields according to dhcp switch setting.