package com.codegym.task.task18.task1828;

/*
Prices 2

*/

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

public class Solution {
    public static void main(String[] args) throws IOException {
        //The program should read a file name for CrUD operations from the console.
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String fileName = reader.readLine();
        reader.close();

        //setup a copy of the files data
        HashMap<String, String> map = new LinkedHashMap<>();
        FileInputStream fileInputStream = new FileInputStream(fileName);
        BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(fileInputStream));
        while(bufferedInputStream.ready()) {
            String line = bufferedInputStream.readLine();
            String id = line.substring(0,8);
            String data = line.substring(8);
            map.put(id,data);
        }
        fileInputStream.close();
        bufferedInputStream.close();
        //When you run the program without arguments, the product list must remain unchanged.
        if(!args[0].isEmpty()){
            //When the program is started with the arguments "-u id productName price quantity", the product information in the file should be updated.
            if(args[0].equals("-u")){
                StringBuilder id = new StringBuilder(String.valueOf(args[1]));
                StringBuilder productName = new StringBuilder(args[2]);
                StringBuilder price = new StringBuilder(String.valueOf(Double.parseDouble(args[3])));
                StringBuilder quantity = new StringBuilder(String.valueOf(Integer.parseInt(args[4])));
                while (id.length() < 8) {
                    id.append(" ");
                }
                while (productName.length() < 30) {
                    productName.append(" ");
                }
                while (price.length() < 8) {
                    price.append(" ");
                }
                while (quantity.length() < 4) {
                    quantity.append(" ");
                }
                String updateData = productName.toString()+price.toString()+quantity.toString();
                //update value in file copy
                map.replace(String.valueOf(id), updateData);
                //update file with file copy
                writeToFile(fileName,map);
            }
            //When the program is started with the arguments "-d id", the line for the product with the specified id should be removed from the file.
            if(args[0].equals("-d")){
                StringBuilder id = new StringBuilder(String.valueOf(args[1]));
                while(id.length()<8) { id.append(" "); }
                map.remove(String.valueOf(id));
                //update file with file copy
                writeToFile(fileName,map);
            }
        }
    }
    public static void writeToFile(String fileName, HashMap<String, String> map) throws IOException {
        //update file with file copy
        Writer fileWriter = new FileWriter(fileName,false);
        for(String key : map.keySet()){
            fileWriter.write(key+ map.get(key));
        }
        fileWriter.close();
    }
}