package model;

import java.io.*;
import java.util.ArrayList;

public class TitanDSM {
    private int sizeOfMatrix;
    private ArrayList<ArrayList<Boolean>> dataMatrix;
    private ArrayList<String> nameOfClass;

    public TitanDSM(int sizeOfMatrix) throws NotPositiveException {
        if (sizeOfMatrix <= 0) {
            throw new NotPositiveException();
        }
        this.sizeOfMatrix = sizeOfMatrix;
        dataMatrix = new ArrayList<>();
        nameOfClass = new ArrayList<>();
        initDataMatrix();
        initNameOfClass();
    }

    public TitanDSM(File file) throws IOException, WrongDSMFormatException {
        dataMatrix = new ArrayList<>();
        nameOfClass = new ArrayList<>();
        loadFromFile(file);
    }

    private void initNameOfClass() {
        for(int i = 1; i <= sizeOfMatrix; i++) {
            nameOfClass.add("entity_" + i);
        }
    }

    private void initDataMatrix() {
        for(int i = 0; i < sizeOfMatrix; i++) {
            dataMatrix.add(new ArrayList<>());
            for(int j = 0; j < sizeOfMatrix; j++) {
                dataMatrix.get(i).add(false);
            }
        }
    }

    public int getSize() {
        return sizeOfMatrix;
    }

    public Boolean getData(int row, int col) {
        return dataMatrix.get(row).get(col);
    }

    public void setData(boolean data, int row, int col) {
        dataMatrix.get(row).set(col, data);
    }

    public String getName(int index) {
        return nameOfClass.get(index);
    }

    public void setName(String newName, int index) throws ItemAlreadyExistException {
        if(nameOfClass.contains(newName)) {
            throw new ItemAlreadyExistException();
        } else {
            nameOfClass.set(index, newName);
        }
    }

    public int getIndexByName(String name) {
        return nameOfClass.indexOf(name);
    }

    public void addEntity(){
        sizeOfMatrix++;
        nameOfClass.add("entity" + sizeOfMatrix);
        dataMatrix.add(new ArrayList<>());
        for(int i = 0;i < sizeOfMatrix; i++){
            dataMatrix.get(i).add(false);
            dataMatrix.get(sizeOfMatrix - 1).add(false);
        }
        dataMatrix.get(sizeOfMatrix - 1).remove(sizeOfMatrix);
    }

    public void loadFromFile(File dsm) throws IOException, WrongDSMFormatException {
        BufferedReader fileReader = new BufferedReader(new FileReader(dsm));
        String read;
        String[] temp;

        read = fileReader.readLine();
        sizeOfMatrix = Integer.parseInt(read);

        for (int i = 0; i < sizeOfMatrix; i++) {
            read = fileReader.readLine();
            temp = read.split(" ");
            dataMatrix.add(new ArrayList<>());
            for (int j = 0; j < sizeOfMatrix; j++) {
                dataMatrix.get(i).add(Integer.parseInt(temp[j]) == 1);
            }
        }

        for (int i = 0; i < sizeOfMatrix; i++) {
            read = fileReader.readLine();
            nameOfClass.add(read);
        }

        fileReader.close();
    }

    public void saveToFile(String fileName) throws IOException{
        BufferedWriter out = new BufferedWriter(new FileWriter(fileName));

        out.write(String.valueOf(sizeOfMatrix));
        out.newLine();

        for(int i = 0; i < sizeOfMatrix; i++) {
            for(int j = 0; j < sizeOfMatrix; j++) {
                if(dataMatrix.get(i).get(j) == Boolean.TRUE) {
                    out.write("1 ");
                        } else {
                    out.write("0 ");
                }
            }
            out.newLine();
        }

        for(int i = 0; i < sizeOfMatrix; i++) {
            out.write(nameOfClass.get(i));
            out.newLine();
        }

        out.close();
    }

}

@@ -2466,32 +2466,125 @@
 der.readLine();%0A
+        if(isNum(read) == false)%7B%0A            throw new WrongDSMFormatException();%0A        %7D%0A
         sizeOfMa
@@ -2611,24 +2611,25 @@
 Int(read);%0A%0A
+%0A
         for 
@@ -2759,315 +2759,956 @@
     
-dataMatrix.add(new ArrayList%3C%3E());%0A            for (int j = 0; j %3C sizeOfMatrix; j++) %7B%0A                dataMatrix.get(i).add(Integer.parseInt(temp%5Bj%5D) == 1);%0A            %7D%0A        %7D%0A%0A        for (int i = 0; i %3C sizeOfMatrix; i++) %7B%0A            read = fileReader.readLine();%0A            nameOfClass.add(read
+if(temp.length != sizeOfMatrix)%7B%0A                throw new WrongDSMFormatException();%0A            %7D%0A            dataMatrix.add(new ArrayList%3C%3E());%0A            for (int j = 0; j %3C sizeOfMatrix; j++) %7B%0A                if(temp%5Bj%5D.equals(%220%22) == false && temp%5Bj%5D.equals(%221%22) == false)%7B%0A                    throw new WrongDSMFormatException();%0A                %7D%0A                dataMatrix.get(i).add(Integer.parseInt(temp%5Bj%5D) == 1);%0A            %7D%0A        %7D%0A%0A        for (int i = 0; i %3C sizeOfMatrix; i++) %7B%0A            temp = read.split(%22 %22);%0A            if(temp.length != 1)%7B%0A                throw new WrongDSMFormatException();%0A            %7D%0A%0A            try %7B%0A                read = fileReader.readLine();%0A            %7D%0A            catch(IOException e)%7B%0A                throw new WrongDSMFormatException();%0A            %7D%0A            nameOfClass.add(read);%0A        %7D%0A%0A        if(fileReader.read() != -1)%7B%0A            throw new WrongDSMFormatException(
 );%0A 
@@ -4182,32 +4182,48 @@
                %7D
+%0A               
  else %7B%0A        
@@ -4478,10 +4478,104 @@
 %0A    %7D%0A%0A
+    private Boolean isNum(String toCheck)%7B%0A        return toCheck.matches(%22%5B1-9%5D%5C%5Cd*%22);%0A    %7D%0A
 %7D%0A

apply WrongDSMFormatException