package cbd.redis;

import java.util.Scanner;
import java.util.List;
import redis.clients.jedis.Jedis;



public class B_Atendimento {

    private Jedis jedis;
    private static int limit = 20; //products limit
    private static int timeslot = 2 * 60; //time limit (s)
    

    public B_Atendimento() {
        this.jedis = new Jedis();
    } 

    public void insertProducts(String username, String prod, int amount) {

        String key = "atendimentosB:" + username;

        if (!jedis.exists(key)) {
            if (amount > limit) {
                System.out.println("Product amount limit reached for the time interval, only registered " + limit + " units");
                jedis.hincrBy(key, prod, limit);
                jedis.expire(key, timeslot);
                System.out.println("Please wait " + (jedis.ttl(key)) + " seconds to add more products, or press 'Enter' to quit");
                return;
            }
            else{
                jedis.hincrBy(key, prod, amount);
                jedis.expire(key, timeslot);
                System.out.println("Product " + prod + " is now registered (amount: " + amount + ")");
                return;
            } 
        }

        else {
            List<String> numProd = jedis.hvals(key);
            int total = 0;

            for (String amnt : numProd) {
                total += Integer.parseInt(amnt);
            }
        
            if (total == limit){
                System.out.println("Product limit reached for the time interval");
                System.out.println("Please wait " + (jedis.ttl(key)) + " seconds to add more products, or press 'Enter' to quit");
                return;
            }
            else{
                if (total + amount > limit){
                    System.out.println("Product amount limit reached for the time interval, only registered " + (limit - total) + " units");
                    jedis.hincrBy(key, prod, limit - total);
                    System.out.println("Please wait " + (jedis.ttl(key)) + " seconds to add more products, or press 'Enter' to quit");
                    return;
                }
                else{
                    if (jedis.hexists(key, prod)) {
                        System.out.println("Product " + prod + " already registered. Amount updated (total amount: " + (amount + Integer.parseInt(jedis.hget(key, prod))) + ")");
                        jedis.hincrBy(key, prod, amount);
                        return;
                    }
                    else{
                        jedis.hincrBy(key, prod, amount);
                        System.out.println("Product " + prod + " is now registered (amount: " + amount + ")");
                        return;
                    }
                }
              
            }
        }
    }   
        
       
    public static void main(String[] args) {
        B_Atendimento a = new B_Atendimento();
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please write your username in order to register products: ");
        String username = scanner.nextLine();
        System.out.print("Please write (product,amount) you would like to register (press 'Enter' to quit): ");
        String line = scanner.nextLine();
        
        while (!line.isEmpty()) {
            String[] parts = line.split(",");

            if (parts.length != 2){
                System.err.println("Invalid input, please write (product,amount). Exiting");
                System.exit(1);
            }

            String prod = parts[0];
            int amount = Integer.parseInt(parts[1]);
    

            a.insertProducts(username, prod, amount);
            line = scanner.nextLine();
            parts = line.split(",");

            if (parts.length != 2 && !line.isEmpty()){
                System.err.println("Invalid input, please write (product,amount). Exiting");
                System.exit(1);

            }
            else if (parts.length == 2){
                
                prod = parts[0];
                amount = Integer.parseInt(parts[1]);
            }

        
        }

    
        System.out.println("Exiting...");

        scanner.close();    
        a.jedis.close();	
        
    }
}




