package manufacturing;

import java.util.ArrayList;
import java.util.Collections;

import sygr.pots.extensions.Attr;
import sygr.pots.extensions.BusinessParameter;
import sygr.pots.extensions.PluginUtilInterface;

public class Util {
	
	public static Attr getPoAttr(String podata, PluginUtilInterface util) {
		Attr attr = new Attr();
		
		ArrayList<String> lines = new ArrayList<>();
		try {
			String[] pts = podata.split(System.lineSeparator());
			Collections.addAll(lines, pts);
		} catch (Exception e) {
			util.log("GetPoAttr: Raw PO data could not split into lines.");
			return attr;
		}
		
		for(String line: lines) {
			line = line + ";";
			
			try {
				String[] pts = line.split(";");
				if(pts == null || pts.length == 0) continue;
				
				pts[0] = pts[0] + "=";
				String[] ptss = pts[0].split("=");
				if(ptss == null || ptss.length < 2) continue;
				
				util.log("GetPoAttr: processing line: " + ptss[0] + " = " + ptss[1]);
				if(ptss[0].toUpperCase().contains("ATERIA")) {
					//util.setNodeValue(attr, ptss[1], "items." + ptss[0]);
					//util.log("GetPoAttr: node value set.");
					if(pts.length >= 2) {
						pts[1] = pts[1] + "=";
						String[] ptss2 = pts[1].split("=");
						if(ptss2 == null || ptss2.length < 2) continue;
						util.log("GetPoAttr: processing line: " + ptss2[0] + " = " + ptss2[1]);
						util.setNodeValue(attr, ptss2[1], "items." + ptss[1]);
						util.log("GetPoAttr: node value set.");
					}
				}
				else {
					util.setNodeValue(attr, ptss[1], ptss[0]);
					util.log("GetPoAttr: node value set.");
				}
				
				
			} catch(Exception e) {
				util.log("GetPoAttr: error reading line");
			}
		}
		
		return attr;
	}
	
	public static String getPrompt(String text) {
		String prompt = "Text: " + text + "\\n";
		prompt = prompt + "You are a professional company sales representative. ";
		prompt = prompt + "From the above purchase order text extract the following information: ";
		prompt = prompt + "the customer name, company name, street address, city, ZIP code ";
		prompt = prompt + "and from every item the material name and the quantity. ";
		prompt = prompt + "Do not include any explanations, only provide the answer like:\\n";
		prompt = prompt + "name = the name of the customer\\n";
		prompt = prompt + "company = the name of the company\\n";
		prompt = prompt + "street = the street address\\n";
		prompt = prompt + "city = the city\\n";
		prompt = prompt + "zip = the ZIP code\\n";
		prompt = prompt + "material = the material; quantity = the quantity;\\n";
		prompt = prompt + "material = the material; quantity = the quantity;\\n";
		prompt = prompt + "material = the material; quantity = the quantity;\\n";
		prompt = prompt + "material = the material; quantity = the quantity;\\n";
		
		
		return prompt;
	}

	public static String getPdfTxtUrl(PluginUtilInterface util) {
		util.log("getPdfTxtUrl: start");
		ArrayList<BusinessParameter> bps = util.getBusinessParameters(
				"manufacturing", "util", "pdftotxt", "");
		if(bps == null || bps.size() == 0) {
			util.log("getPdfTxtUrl: business parameter not found");
			return "";
		}
		BusinessParameter bp = bps.get(0);
		util.log("getPdfTxtUrl: found URL: " + bp.getValue());
		return bp.getValue();
	}
		
	public static String getNextDocNumber(PluginUtilInterface util) {
		int soNumber = 0;
		
		// Try to get the last SO number
		String lastNumber = util.getParameter(Constants.DOCNOAPP, Constants.DOCNOPARAM);
		
		// If not found, initialize
		if(lastNumber.equals("")) {
			soNumber = 1000000000;
		}
		// If found, convert to integer
		else {
			soNumber = Integer.parseInt(lastNumber);
		}
		
		// Increase
		soNumber++;
		
		// Save
		String newNumber = String.valueOf(soNumber);
		util.setParameter(Constants.DOCNOAPP, Constants.DOCNOPARAM, newNumber);
		
		return newNumber;
	}
	
}
