package com.example.sdksamples;

import com.impinj.octane.*;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class QueryReaderSettings {

    // Create an instance of the ImpinjReader class.
    static ImpinjReader reader = new ImpinjReader();

    static void DisplayCurrentSettings() throws OctaneSdkException {
        // Query the current reader settings and print some of the results.
        System.out.println("Reader Settings");
        System.out.println("---------------");

        Settings settings = reader.querySettings();
        System.out.println("RF mode : " + settings.getRfMode());
        System.out.println("Search mode : " + settings.getSearchMode());
        System.out.println("Session : " + settings.getSession());
        System.out.println("Impinj regulatory region : " + settings.getImpinjRegulatoryRegion());

        ArrayList<AntennaConfig> ac = settings.getAntennas().getAntennaConfigs();

        if (ac.get(0).getIsMaxRxSensitivity()) {
            System.out.println("Rx sensitivity (Antenna 1) : Max");
        } else {
            System.out.println("Rx sensitivity (Antenna 1) : "
                    + ac.get(0).getRxSensitivityinDbm() + " dBm");
        }

        if (ac.get(0).getIsMaxTxPower()) {
            System.out.println("Tx power (Antenna 1) : Max");
        } else {
            System.out.println("Tx power (Antenna 1) : "
                    + ac.get(0).getTxPowerinDbm() + " dBm");
        }

        System.out.println("");
    }

    public static void main(String[] args) {

        try {
            String hostname = System.getProperty(SampleProperties.hostname);
            String folderPath = System.getProperty(SampleProperties.folderPath);
            if (folderPath == null) {
                folderPath = "";
            }

            if (hostname == null) {
                throw new Exception("Must specify the '" + SampleProperties.hostname + "' property");
            }

            reader.connect(hostname);

            // We want to print a unicode character in the output, so must use a PrintStream
            // other than System.out.
            PrintStream ps = new PrintStream(new FileOutputStream(FileDescriptor.out), true, "UTF-8");

            // Query the reader features and print the results.
            ps.println("Reader Features");
            ps.println("---------------");
            FeatureSet features = reader.queryFeatureSet();
            ps.println("Model name : " + features.getModelName());
            ps.println("Model number : " + features.getModelNumber());
            ps.println("Reader model : " + features.getReaderModel().toString());
            ps.println("Firmware version : " + features.getFirmwareVersion());
            ps.println("Antenna count : " + features.getAntennaCount());
            ps.println("Communications standard : " + features.getCommunicationsStandard());
            List<TxPowerTableEntry> txPowers = features.getTxPowers();
            ps.println("Max tx power : " + ((txPowers != null && !txPowers.isEmpty()) ?
                    txPowers.get(txPowers.size() - 1).Dbm : 0) + " dBm\n");

            // Write the reader features to file.
            features.save(folderPath + "features.xml");

            // Query the current reader status.
            ps.println("Reader Status");
            ps.println("---------------");
            Status status = reader.queryStatus();
            ps.println("Is connected : " + status.getIsConnected());
            ps.println("Is singulating : " + status.getIsSingulating());
            ps.println("Temperature : " + status.getTemperatureCelsius() + "\u00B0 C");
            ps.println("");

            // Configure the reader with the default settings.
            reader.applyDefaultSettings();

            // Display the current reader settings.
            DisplayCurrentSettings();

            // Save the settings to file in XML format.
            ps.println("Saving settings to file.");
            Settings settings = reader.querySettings();
            settings.save(folderPath + "settings.xml");

            // Wait here, so we can edit the
            // settings.xml file in a text editor.
            ps.println("Edit settings.xml and press enter.");
            Scanner s = new Scanner(System.in);
            s.nextLine();

            // Load the modified settings from file.
            ps.println("Loading settings from file.");
            settings = Settings.load(folderPath + "settings.xml");

            // Apply the settings we just loaded from file.
            ps.println("Applying settings from file.\n");
            reader.applySettings(settings);

            // Display the settings again to show the changes.
            DisplayCurrentSettings();

            // Wait for the user to press enter.
            ps.println("Press enter to exit.");
            s.nextLine();
            s.close();

            // Disconnect from the reader.
            reader.disconnect();
        } catch (OctaneSdkException ex) {
            System.out.println("Octane SDK exception: " + ex.getMessage());
        } catch (java.io.IOException ex) {
            System.out.println("Exception : " + ex.getMessage());
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.getMessage());
            ex.printStackTrace(System.out);
        }
    }
}
