package com.juanan.pocs.companies.core.utils;

import org.apache.commons.lang3.StringUtils;

public class CompanyUtils {

	public static String getCompanyNameWithoutErrors(String company){
		String companyName = new String(company);
    	// Al principio llegaban los nombres de empresas sin un punto al final
    	if (companyName.endsWith(".")){
    		companyName = companyName.substring(0, companyName.length()-1);
    	}
		if (companyName.endsWith("(R.M") || companyName.endsWith("(r.m")){
			companyName = companyName.substring(0, companyName.length()-"(R.M".length());
		}
		if (companyName.endsWith(" EN LIQUIDACION") || companyName.endsWith(" en liquidacion")){
			companyName = companyName.substring(0, companyName.length()-" EN LIQUIDACION".length());
		}
		return companyName;
	}
	
	public static String getCompanyFormatInfoCIF(String company){
		String formatCompany = new String(company);
		formatCompany = formatCompany.toLowerCase();
		/*if (formatCompany.endsWith("sociedad limitada")){
			formatCompany = formatCompany.substring(0, formatCompany.length()-"sociedad limitada".length());
			formatCompany = formatCompany.concat("sl");
		}
		if (formatCompany.endsWith("sociedad anonima")){
			formatCompany = formatCompany.substring(0, formatCompany.length()-"sociedad anonima".length());
			formatCompany = formatCompany.concat("sa");
		}*/
		formatCompany = formatCompany.replaceAll("sociedad limitada","sl");
		formatCompany = formatCompany.replaceAll("sociedad anonima","sa");
		/*if (formatCompany.endsWith("(r.m")){
			formatCompany = formatCompany.substring(0, formatCompany.length()-"(r.m".length());
		}
		if (formatCompany.endsWith(" en liquidacion")){
			formatCompany = formatCompany.substring(0, formatCompany.length()-" en liquidacion".length());
		}*/
		formatCompany = formatCompany.replaceAll(" ", "-");
		formatCompany = formatCompany.replaceAll(",", "");
		formatCompany = formatCompany.replaceAll("&", "");
		formatCompany = formatCompany.replaceAll("\\.", "");
		formatCompany = formatCompany.replaceAll("\\'", "");
		formatCompany = org.apache.commons.lang3.StringUtils.stripAccents(formatCompany);		
		return formatCompany;
	}
	
	public static String getRegionFormattedName(String region){
		String formatProvincia = null;
		if (region != null){
			formatProvincia = new String(region);
			formatProvincia = formatProvincia.toUpperCase();
			formatProvincia = formatProvincia.replaceAll(",", "");
			formatProvincia = org.apache.commons.lang3.StringUtils.stripAccents(formatProvincia);
		}		
		return formatProvincia;
	}
	
	public static String getCompanyFolderName(String company){
		String folder = "";
		if (StringUtils.isNotBlank(company)){
			folder = company.replaceAll(" ","").substring(0,2).toUpperCase();
		}
		return folder;
	}
	
}
