package Basket;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by K_A_Volchica on 20.03.2015.
 */
public class BasketImpl implements Basket {

    List<String> get=new ArrayList<String>();

    Map<String, Integer> content=new HashMap<String, Integer>();


    @Override
    public void addProduct(String product, int quantity) {
        if (product == null || product.trim().length() == 0) {
            System.out.println("Product cannot be null");
            System.out.println(": get= "+get+"; "+"content="+content);
            return;
        }
        if (quantity==0){
            System.out.println("Quantity cannot be null");
            System.out.println(": get= "+get+"; "+"content="+content);
            return;
        }
        if (quantity < 0) {
            System.out.println("Quantity should be positive, go to update");
            updateProductQuantity(product,quantity);
            return;
        }
        if (content.containsKey(product)) {
            System.out.println("Quantity++, go to update");
            updateProductQuantity(product,quantity);
            return;
        }
        else {
            get.add(product);
            content.put(product, quantity);
            System.out.println("Add: get= "+get+"; "+"content="+content);
            return;
        }
    }

    @Override
    public void removeProduct(String product) {
        if (product == null || product.trim().length() == 0) {
            System.out.println("Product cannot be null");
            System.out.println(": get= "+get+"; "+"content="+content);
            return;
        }
        if (content.containsKey(product) & content.containsValue(0)) {
            System.out.println("Quantity should be positive");
            System.out.println(": get= "+get+"; "+"content="+content);
            return;
        }
        if (content.containsKey(product)) {
            content.remove(product);
            get.remove(product);
            System.out.println("Remove: get= "+get+"; "+"content="+content);
        }
    }

    @Override
    public void updateProductQuantity(String product, int quantity) {
        if (product == null || product.trim().length() == 0) {
            System.out.println("Product cannot be null");
            System.out.println(": get= "+get+"; "+"content="+content);
            return;
        }
        if (content.containsKey(product)) {
            if (quantity==0){
                System.out.println("Quantity cannot be null");
                System.out.println(": get= "+get+"; "+"content="+content);
                return;
            }
            if (quantity < 0)
            {
                if (content.get(product)<-quantity)
                {
                    System.out.println("Quantity cannot be negative");
                    System.out.println(": get= "+get+"; "+"content="+content);
                    return;
                }
                if (content.get(product)==-quantity)
                {
                    System.out.println("Quantity cannot be null, go to remove");
                    removeProduct(product);
                    return;
                }
                else
                {
                    quantity=content.get(product)+quantity;
                    content.put(product,quantity);
                    System.out.println("Update: get= "+get+"; "+"content="+content);
                    return;
                }
            }
            else
            {
                quantity+=content.get(product);
                content.put(product,quantity);
                System.out.println("Update: get= "+get+"; "+"content="+content);
                return;
            }
        }
    }


    @Override
    public void clear() {
        content = null;
        get=null;
    }

    @Override
    public List<String> getProducts() {
        System.out.println("List getProducts"+get);
        return get;
    }

    @Override
    public int getProductQuantity(String product) {
        int a=0;
        if (product == null || product.trim().length() == 0) {
            System.out.println("Product cannot be null");
            return a;
        }
        else {
            a=content.get(product);
        }
        System.out.println("getProductQuantity: product= "+product+"; "+"quantity="+a);
        return a;
    }

}

