package com.example.sdksamples;

import com.impinj.octane.*;

import java.util.Scanner;

public class TagPopulationEstimationAlgorithm {

    public static void main(String[] args) {

        try {
            // Pass in a reader hostname or IP address as a command line argument when running the example.
            String hostname = System.getProperty(SampleProperties.hostname);
            if (hostname == null) {
                throw new Exception("Must specify the '" + SampleProperties.hostname + "' property");
            }
            ImpinjReader reader = new ImpinjReader();

            // Connect to the reader.
            System.out.println("Connecting");
            reader.connect(hostname);

            // Get the default settings.
            // We'll use these as a starting point and then modify the settings we're interested in.
            Settings settings = reader.queryDefaultSettings();

            // If ReaderDefault is coming back from queryDefaultSettings, the firmware doesn't support tag population
            // estimation and we can't run this example.
            if (settings.getTagPopulationEstimationAlgorithm() == TagPopulationEstimationMode.ReaderDefault)
            {
                reader.disconnect();
                throw new Exception("This example can only be demonstrated on 8.0 firmware and above");
            }

            ReportConfig report = settings.getReport();

            // Tell the reader to include the antenna number in all tag reports. Other fields can be added
            // to the reports in the same way by setting the appropriate Report.IncludeXXXXXXX property.
            report.setIncludeAntennaPortNumber(true);
            report.setMode(ReportMode.Individual);

            // The reader can be set into various modes in which reader dynamics are optimized for
            // specific regions and environments.
            // The following mode, AutoSetDenseReaderDeepScan, monitors RF noise and interference
            // and then automatically and continuously optimizes the reader's configuration.
            settings.setRfMode(1002);
            settings.setSearchMode(SearchMode.DualTarget);
            settings.setSession(2);

            // This population estimate should be much too large and TagPopulationEstimation should be explicitly
            // disabled.
            settings.setTagPopulationEstimate(1000);
            settings.setTagPopulationEstimationAlgorithm(TagPopulationEstimationMode.Disabled);

            // Enable antenna #1. Disable all others.
            AntennaConfigGroup antennas = settings.getAntennas();
            antennas.disableAll();
            antennas.enableById(new short[]{1});

            // Set the Transmit Power and
            // Receive Sensitivity to the maximum.
            antennas.getAntenna((short) 1).setIsMaxRxSensitivity(true);
            antennas.getAntenna((short) 1).setIsMaxTxPower(true);

            // You can also set them to specific values like this...
            // antennas.getAntenna((short) 1).setTxPowerinDbm(20.0);
            // antennas.getAntenna((short) 1).setRxSensitivityinDbm(-70);

            // Apply the newly modified settings.
            System.out.println("Applying Settings");
            reader.applySettings(settings);

            // Assign the TagsReported event listener.
            // This specifies which object to inform when tags reports are available.
            TagReportCounterImplementation tagCounter = new TagReportCounterImplementation();
            reader.setTagReportListener(tagCounter);

            // Start reading.
            System.out.println("Starting");
            reader.start();

            Thread.sleep(5000);

            reader.stop();

            // Let tag reads finish
            Thread.sleep(1000);

            int badEstimateReadCount = tagCounter.getCount();
            tagCounter.setCount(0);

            // Now turn Tag Population Estimation Algorithm on
            settings.setTagPopulationEstimationAlgorithm(TagPopulationEstimationMode.Enabled);
            reader.applySettings(settings);

            System.out.println("Read count with user-provided tag population estimate: " + badEstimateReadCount);
            System.out.println("Now applying Tag Population Estimation Algorithm");
            Thread.sleep(2000);

            reader.start();

            // Let reader run for 5 seconds
            Thread.sleep(5000);

            reader.stop();

            // Let tag reads finish
            Thread.sleep(1000);

            int estimationAlgorithmReadCount = tagCounter.getCount();

            System.out.println("Read count with user-provided tag population estimate: " + badEstimateReadCount);
            System.out.println("Read count with user-provided tag population estimate: " + estimationAlgorithmReadCount);

            // Wait for the user to press enter.
            System.out.println("Press Enter to exit.");
            Scanner s = new Scanner(System.in);
            s.nextLine();

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