/**
   * Methode : saveAccreditedInformation() allows you to save all the information about the
   * accredited user in a ser file.
   *
   * @param data : contains the data received in the serial port
   * @return true if the owner information is correct
   * @throws java.lang.InterruptedException
   */
  private boolean saveAccreditedInformation(String[] data) throws InterruptedException {
    boolean flag = false;

    if (super.isChecksumCorrect(data)) {
      OwnerInformation user = new OwnerInformation(data);
      // Make the Serialization before closing the windows
      File file = new File("accredited_information.ser");
      try {
        try (FileOutputStream fileOut = new FileOutputStream(file);
            ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
          out.writeObject(user);
          System.out.println(user);
          flag = true;
          System.out.println("accredited_information.ser file is created correcly");
        }
      } catch (IOException i) {
        System.out.println("Exception de Serialisation " + i.getMessage());
        flag = false;
      }
    } else {
      System.out.println("The data about the accredited user is not saved");
      flag = false;
    }

    return flag;
  }
  /**
   * Methode : addDeviceLink() allows you to add the device in the table which containing the id and
   * the key information.
   *
   * @param data
   * @return true if the owner information is correct
   * @throws java.lang.InterruptedException
   */
  private boolean addDeviceLink(String[] data) throws InterruptedException {

    boolean flag = false;
    boolean flag_checksum = false;
    boolean flag_extract_user = false;
    boolean flag_extract_device = false;
    boolean flag_creating_device = false;
    boolean flag_serialization = false;
    boolean flag_end = false;

    int checksum_received = 0;
    int checksum_calcule = 0;

    int key_size = 0;

    String id_device = "";
    OwnerInformation accredited_user = null;
    DeviceLinkingData device_linking = null;

    DeviceLinkedData device_linked = null;

    IdentifiantAndKeyTable table_temp = null;

    if (data != null && data.length >= 2) {
      key_accredited = data[0];
      key_size = key_accredited.length();

      // Extract the checksul and convert it
      try {
        checksum_received = Integer.parseInt(data[1]);
      } catch (NumberFormatException ex) {
        checksum_received = 0;
        System.out.println("Impossible de convert String to Intger : " + ex.getMessage());
      }

      // Calcul du checksul
      checksum_calcule = (key_size * 128) + (key_size / 2) * 64 + (key_size / 4) * 32;
      checksum_calcule += (key_size / 8) * 16 + (key_size / 16) * 8 + (key_size / 32) * 4;
      checksum_calcule += (key_size / 64) * 2;

    } else {
      System.out.println("L'argument passé en paramètre est invalide");
      flag = false;
    }

    flag_checksum = checksum_calcule == checksum_received;

    // Test if the checksum is correct or not
    if (flag_checksum) {
      System.out.println("CRC Correct");

      File file = new File("accredited_information.ser");
      // Deserialization otf the OwnerInformation
      try (FileInputStream fileIn = new FileInputStream(file);
          ObjectInputStream in = new ObjectInputStream(fileIn)) {
        accredited_user = (OwnerInformation) in.readObject();
        flag_extract_user = true;

      } catch (IOException i) {
        flag_extract_user = false;
        System.out.println("IOException : " + i.getMessage());
      } catch (ClassNotFoundException c) {
        flag_extract_user = false;
        System.out.println("OwnerInformation class not found " + c.getMessage());
      }

      file = new File("linking_test.ser");
      // Deserialization of the DeviceLinkingInfo
      try (FileInputStream fileIn = new FileInputStream(file);
          ObjectInputStream in = new ObjectInputStream(fileIn)) {
        device_linking = (DeviceLinkingData) in.readObject();
        System.out.println("DEVICE LINKING DEBUG = \n" + device_linking);
        flag_extract_device = true;

      } catch (IOException i) {
        flag_extract_device = false;
        System.out.println("IOException : " + i.getMessage());
      } catch (ClassNotFoundException c) {
        flag_extract_device = false;
        System.out.println("DeviceLinkingData class not found " + c.getMessage());
      }
    } else {
      flag_extract_user = false;
      flag_extract_device = false;
      System.out.println("CRC inCorrect");
    }

    // Check if the extraction of the owner is done succesfully, create the class DeviceLinkedData
    if (flag_extract_user && flag_extract_device) {
      id_device = device_linking.getDeviceIdentifiant();

      if (id_device != null) {
        System.out.println("Identifiant du Device partagé = " + id_device);
      } else {
        System.out.println("Identifiant du Device partagé est null");
        id_device = "TOTO";
      }

      // Create the DeviceLinkedData class
      // device_linked = new DeviceLinkedData (user,device_linking, id_device, key);
      // device_linked = new DeviceLinkedData (accredited_user,device_linking, id_device,
      // key_accredited);
      device_linked =
          new DeviceLinkedData(accredited_user, device, id_sesame_sharer, key_accredited);
      flag_creating_device = true;
      System.out.println("DEVICE LINKED DEBUG = \n" + device_linked);
      System.out.println("device_linked.getDeviceId() = " + device_linked.getDeviceId());
    } else {
      flag_creating_device = false;
    }

    // Check if the class "DeviceLinkedData" is created correctly
    if (flag_creating_device) {
      // Make the deserialization of the table file which is the database of the device
      File file = new File("identifiant_and_key_table.ser");
      try (FileInputStream fileIn = new FileInputStream(file);
          ObjectInputStream in = new ObjectInputStream(fileIn)) {
        table_temp = (IdentifiantAndKeyTable) in.readObject();

        // Add the device link information on the table
        table_temp.addDeviceForLink(device_linked);
        System.out.println("Content of the table after");
        System.out.println(table_temp);

        flag_serialization = true;

      } catch (IOException i) {
        flag_serialization = false;
        System.out.println("IOException : " + i.getMessage());
      } catch (ClassNotFoundException c) {
        flag_serialization = false;
        System.out.println("IdentifiantAndKeyTable class not found");
      }
    } else {
      System.out.println(
          "'identifiant_and_key_table.ser' file is not found in the current directory");
      flag_serialization = false;
    }

    // Check if the flag serialization is correct then make the serialization in the file
    if (flag_serialization) {
      // Make the serialization to save the new added device on the table
      File file = new File("identifiant_and_key_table.ser");
      try (FileOutputStream fileOut = new FileOutputStream(file);
          ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
        out.writeObject(table_temp);
        // End of the Serialization for IdentifiantAndKeyTable
        System.out.println(
            "Serialized of the new IdentifiantAndKeyTable is saved in identifiant_and_key_table.ser");
        flag_end = true;
      } catch (IOException io) {
        flag_end = false;
        System.out.println("Exception for IdentifiantAndKeyTable : " + io.getMessage());
        System.out.println(
            "Serialized of the new IdentifiantAndKeyTable is not done : check error");
      }
    } else {
      flag_end = false;
      System.out.println(
          "impossible to read the 'identifiant_and_key_table.ser' file or the file is not found");
    }

    return flag_end;
  }