package page.tools.xml;

import java.util.Collection;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.wikiwebserver.core.WareHouse;

public class ProgramDetails {

    private String name;
    private String version;
    private long releaseDate;
    private int costInPennies;
    private String costCurr = "GBP";
    // GBP to USD as of 2006-02-12
    private float pennyToCent = 1.94600f; 
    private String type = "Freeware";
    private String releaseStatus = "New Release";
    private String installSupport = "Install and Uninstall";
    private String osSupport = "Java";
    private String language = "English";
    private String recentChangeInfo;
    private String category;
    private String categoryClass;
    private String requirements;
    
    private String filenameVersioned;
    private String filenamePrevious;
    private String filenameGeneric;
    private String filenameLong;
    private long fileSizeInBytes;
    
    private Collection<String> keywords;
    private String description45;
    private String description80;
    private String description250;
    private String description450;
    private String description2000;
    
    private String informationURL;
    private String orderURL;
    private String screenshotURL;
    private String iconURL;
    private String padURL;
    
    private String downloadURL;
    private String downloadURL2;
    private String downloadURL3;
    private String downloadURL4;
    
    private String distributionPermissions;
    private String EndUserLicenceAgreement;
    
    
    public Node getInfoNode(Document doc) {
        
        Element program = doc.createElement("Program_Info");
        
        Element name = doc.createElement("Program_Name");
        name.setTextContent(this.getName());
        Element version = doc.createElement("Program_Version");
        version.setTextContent(this.getVersion());

        Element releaseDay = doc.createElement("Program_Release_Day");
        releaseDay.setTextContent(WareHouse.formatDate(getReleaseDate(), "dd"));
        Element releaseMonth = doc.createElement("Program_Release_Month");
        releaseMonth.setTextContent(WareHouse.formatDate(getReleaseDate(), "MM"));
        Element releaseYear = doc.createElement("Program_Release_Year");
        releaseYear.setTextContent(WareHouse.formatDate(getReleaseDate(), "yyyy"));
        Element programCost = doc.createElement("Program_Cost_Other");
        programCost.setTextContent(conv(this.getCostInPennies()));        
        Element programCostCurr = doc.createElement("Program_Cost_Other_Code");
        programCostCurr.setTextContent(costCurr);           
        Element programDollars = doc.createElement("Program_Cost_Dollars");
        int cents = (int) (pennyToCent * this.getCostInPennies());
        programDollars.setTextContent(conv(cents));
        Element programType = doc.createElement("Program_Type");
        programType.setTextContent(this.getType());            
        Element releaseStatus = doc.createElement("Program_Release_Status");
        releaseStatus.setTextContent(this.getReleaseStatus());       
        Element installSupport = doc.createElement("Program_Install_Support");
        installSupport.setTextContent(this.getInstallSupport());  
        Element osSupport = doc.createElement("Program_OS_Support");
        osSupport.setTextContent(this.getOsSupport()); 
        Element language = doc.createElement("Program_Language");
        language.setTextContent(this.getLanguage());     
        Element programChange = doc.createElement("Program_Change_Info");
        programChange.setTextContent(this.getRecentChangeInfo());    
        Element category = doc.createElement("Program_Specific_Category");
        category.setTextContent(this.getCategory()); 
        Element categoryClass = doc.createElement("Program_Category_Class");
        categoryClass.setTextContent(this.getCategoryClass()); 
        Element categories = doc.createElement("Program_Categories");
        Element requirements = doc.createElement("Program_System_Requirements");
        requirements.setTextContent(this.getRequirements()); 
        Element incVm = doc.createElement("Includes_JAVA_VM");
        incVm.setTextContent(String.valueOf(false)); 
        Element incVb = doc.createElement("Includes_VB_Runtime");
        incVb.setTextContent(String.valueOf(false));    
        Element incDx = doc.createElement("Includes_DirectX");
        incDx.setTextContent(String.valueOf(false));  
        
        program.appendChild(name);
        program.appendChild(version);
        program.appendChild(releaseDay);
        program.appendChild(releaseMonth);
        program.appendChild(releaseYear);
        program.appendChild(programCost);
        program.appendChild(programCostCurr);
        program.appendChild(programDollars);
        program.appendChild(programType);
        program.appendChild(releaseStatus);     
        program.appendChild(installSupport);      
        program.appendChild(osSupport);      
        program.appendChild(language);      
        program.appendChild(programChange);      
        program.appendChild(category);   
        program.appendChild(categoryClass);      
        program.appendChild(language);      
        program.appendChild(programChange);      
        program.appendChild(categories);     
        program.appendChild(requirements);    
        program.appendChild(incVm);    
        program.appendChild(incVb);    
        program.appendChild(incDx);    
        
        Element fileInfo = doc.createElement("File_Info");
        
        Element filenameVersioned = doc.createElement("Filename_Versioned");
        filenameVersioned.setTextContent(this.getFilenameVersioned());  
        Element filenamePrevious = doc.createElement("Filename_Previous");
        filenamePrevious.setTextContent(this.getFilenameVersioned());  
        Element filenameLong = doc.createElement("Filename_Long");
        filenameLong.setTextContent(this.getFilenameLong());  
        Element fileSizeBytes = doc.createElement("File_Size_Bytes");
        fileSizeBytes.setTextContent(String.valueOf(this.getFileSizeInBytes())); 
        Element fileSizeK = doc.createElement("File_Size_K");
        fileSizeK.setTextContent(String.valueOf(this.getFileSizeInBytes() / 1024)); 
        Element fileSizeMB = doc.createElement("File_Size_MB");
        fileSizeMB.setTextContent(String.valueOf(this.getFileSizeInBytes() / (1024 * 1024)));         
        
        fileInfo.appendChild(filenameVersioned);      
        fileInfo.appendChild(filenamePrevious);      
        fileInfo.appendChild(filenameLong);     
        fileInfo.appendChild(fileSizeBytes);    
        fileInfo.appendChild(fileSizeK);    
        fileInfo.appendChild(fileSizeMB);

        program.appendChild(fileInfo);

        Element expireInfo = doc.createElement("Expire_Info");
        
        Element incExpire = doc.createElement("Has_Expire_Info");
        incExpire.setTextContent("N"); 
        Element expireCount = doc.createElement("Expire_Count");
        Element expireBasedOn = doc.createElement("Expire_Based_On");
        Element expireOtherInfo = doc.createElement("Expire_Other_Info");
        Element expireDay = doc.createElement("Expire_Day");
        Element expireMonth = doc.createElement("Expire_Month");
        Element expireYear = doc.createElement("Expire_Year");
        
        expireInfo.appendChild(incExpire);   
        expireInfo.appendChild(expireCount);      
        expireInfo.appendChild(expireBasedOn);      
        expireInfo.appendChild(expireOtherInfo);     
        expireInfo.appendChild(expireDay);    
        expireInfo.appendChild(expireMonth);    
        expireInfo.appendChild(expireYear);

        program.appendChild(expireInfo);
        
        return program;
    }
  
    public Node getDescriptionsNode(Document doc) {
        
        Element descriptions = doc.createElement("Program_Descriptions");
        
        Element english = doc.createElement("English");
        
        Element keywords = doc.createElement("Keywords");
        StringBuilder keywordList = new StringBuilder();
        int count = 0;
        for (String keyword : this.getKeywords()) {
            if (count > 0)keywordList.append(", ");
            keywordList.append(keyword);
            count++;
        }
        keywords.setTextContent(keywordList.toString());
        Element char45 = doc.createElement("Char_Desc_45");
        char45.setTextContent(this.getDescription45());
        Element char80 = doc.createElement("Char_Desc_80");
        char80.setTextContent(this.getDescription80());
        Element char250 = doc.createElement("Char_Desc_250");
        char250.setTextContent(this.getDescription250());      
        Element char450 = doc.createElement("Char_Desc_450");
        char450.setTextContent(this.getDescription450());     
        Element char2000 = doc.createElement("Char_Desc_2000");
        char2000.setTextContent(this.getDescription2000());      
        
        english.appendChild(keywords);
        english.appendChild(char45);
        english.appendChild(char80);
        english.appendChild(char250);
        english.appendChild(char450);
        english.appendChild(char2000);
        
        descriptions.appendChild(english);

        return descriptions;
    }
  
  
    public Node getWebInfoNode(Document doc) {
        Element webInfo = doc.createElement("Web_Info");
        
        Element appURLs = doc.createElement("Application_URLs");
        
        Element appInfo = doc.createElement("Application_Info_URL");
        appInfo.setTextContent(this.getInformationURL());
        Element appOrder = doc.createElement("Application_Order_URL");
        appOrder.setTextContent(this.getOrderURL());
        Element appScreenShot = doc.createElement("Application_Screenshot_URL");
        appScreenShot.setTextContent(this.getScreenshotURL());      
        Element appIcon = doc.createElement("Application_Icon_URL");
        appIcon.setTextContent(this.getIconURL());     
        Element appXML = doc.createElement("Application_XML_File_URL");
        appXML.setTextContent(this.getPadURL()); 
        
        appURLs.appendChild(appInfo);
        appURLs.appendChild(appOrder);
        appURLs.appendChild(appScreenShot);
        appURLs.appendChild(appIcon);
        appURLs.appendChild(appXML);
        
        webInfo.appendChild(appURLs);
        
        Element downloadURLs = doc.createElement("Download_URLs");
        
        Element download1 = doc.createElement("Primary_Download_URL");
        download1.setTextContent(this.getDownloadURL()); 
        Element download2 = doc.createElement("Secondary_Download_URL");
        download2.setTextContent(this.getDownloadURL2());
        Element download3 = doc.createElement("Additional_Download_URL_1");
        download3.setTextContent(this.getDownloadURL3());
        Element download4 = doc.createElement("Additional_Download_URL_2");
        download4.setTextContent(this.getDownloadURL4());   
        
        downloadURLs.appendChild(download1);
        downloadURLs.appendChild(download2);
        downloadURLs.appendChild(download3);
        downloadURLs.appendChild(download4);
        
        webInfo.appendChild(downloadURLs);   
        
        return webInfo;
    }

    public Node getPermissionsNode(Document doc) {
        Element perms = doc.createElement("Permissions");
        
        Element dist = doc.createElement("Distribution_Permissions");
        dist.setTextContent(this.getDistributionPermissions());   
        
        Element eula = doc.createElement("EULA");
        eula.setTextContent(this.getEndUserLicenceAgreement());        
        
        perms.appendChild(dist);
        perms.appendChild(eula);
        
        return perms;
    }

    public String getName() {
        return name;
    }
    
    
    public void setName(String name) {
        this.name = name;
    }
    
    
    public String getVersion() {
        return version;
    }
    
    
    public void setVersion(String version) {
        this.version = version;
    }
    
    
    public long getReleaseDate() {
        return releaseDate;
    }
    
    
    public void setReleaseDate(long releaseDate) {
        this.releaseDate = releaseDate;
    }
    
    
    public String getCostCurr() {
        return costCurr;
    }
    
    
    public void setCostCurr(String costCurr) {
        this.costCurr = costCurr;
    }
    
    public String getType() {
        return type;
    }
    
    
    public void setType(String type) {
        this.type = type;
    }
    
    
    public String getReleaseStatus() {
        return releaseStatus;
    }
    
    
    public void setReleaseStatus(String releaseStatus) {
        this.releaseStatus = releaseStatus;
    }
    
    
    public String getInstallSupport() {
        return installSupport;
    }
    
    
    public void setInstallSupport(String installSupport) {
        this.installSupport = installSupport;
    }
    
    
    public String getOsSupport() {
        return osSupport;
    }
    
    
    public void setOsSupport(String osSupport) {
        this.osSupport = osSupport;
    }
    
    
    public String getLanguage() {
        return language;
    }
    
    
    public void setLanguage(String language) {
        this.language = language;
    }
    
    
    public String getRecentChangeInfo() {
        return recentChangeInfo;
    }
    
    
    public void setRecentChangeInfo(String recentChangeInfo) {
        this.recentChangeInfo = recentChangeInfo;
    }
    
    
    public String getCategory() {
        return category;
    }
    
    
    public void setCategory(String category) {
        this.category = category;
    }
    
    
    public String getCategoryClass() {
        return categoryClass;
    }
    
    
    public void setCategoryClass(String categoryClass) {
        this.categoryClass = categoryClass;
    }
    
    
    public String getRequirements() {
        return requirements;
    }
    
    
    public void setRequirements(String requirements) {
        this.requirements = requirements;
    }
    
    
    public String getFilenameVersioned() {
        return filenameVersioned;
    }
    
    
    public void setFilenameVersioned(String filenameVersioned) {
        this.filenameVersioned = filenameVersioned;
    }
    
    
    public String getFilenamePrevious() {
        return filenamePrevious;
    }
    
    
    public void setFilenamePrevious(String filenamePrevious) {
        this.filenamePrevious = filenamePrevious;
    }
    
    
    public String getFilenameGeneric() {
        return filenameGeneric;
    }
    
    
    public void setFilenameGeneric(String filenameGeneric) {
        this.filenameGeneric = filenameGeneric;
    }
    
    
    public String getFilenameLong() {
        return filenameLong;
    }
    
    
    public void setFilenameLong(String filenameLong) {
        this.filenameLong = filenameLong;
    }
    
    
    public long getFileSizeInBytes() {
        return fileSizeInBytes;
    }
    
    
    public void setFileSizeInBytes(long fileSizeInBytes) {
        this.fileSizeInBytes = fileSizeInBytes;
    }
    
    
    public Collection<String> getKeywords() {
        return keywords;
    }
    
    
    public void setKeywords(Collection<String> keywords) {
        this.keywords = keywords;
    }
    
    
    public String getDescription45() {
        return description45;
    }
    
    
    public void setDescription45(String description45) {
        this.description45 = description45;
    }
    
    
    public String getDescription80() {
        return description80;
    }
    
    
    public void setDescription80(String description80) {
        this.description80 = description80;
    }
    
    
    public String getDescription250() {
        return description250;
    }
    
    
    public void setDescription250(String description250) {
        this.description250 = description250;
    }
    
    
    public String getDescription450() {
        return description450;
    }
    
    
    public void setDescription450(String description450) {
        this.description450 = description450;
    }
    
    
    public String getDescription2000() {
        return description2000;
    }
    
    
    public void setDescription2000(String description2000) {
        this.description2000 = description2000;
    }
    
    
    public String getInformationURL() {
        return informationURL;
    }
    
    
    public void setInformationURL(String informationURL) {
        this.informationURL = informationURL;
    }
    
    
    public String getOrderURL() {
        return orderURL;
    }
    
    
    public void setOrderURL(String orderURL) {
        this.orderURL = orderURL;
    }
    
    
    public String getScreenshotURL() {
        return screenshotURL;
    }
    
    
    public void setScreenshotURL(String screenshotURL) {
        this.screenshotURL = screenshotURL;
    }
    
    
    public String getIconURL() {
        return iconURL;
    }
    
    
    public void setIconURL(String iconURL) {
        this.iconURL = iconURL;
    }
    
    
    public String getPadURL() {
        return padURL;
    }
    
    
    public void setPadURL(String padURL) {
        this.padURL = padURL;
    }
    
    
    public String getDownloadURL() {
        return downloadURL;
    }
    
    
    public void setDownloadURL(String downloadURL) {
        this.downloadURL = downloadURL;
    }
    
    
    public String getDownloadURL2() {
        return downloadURL2;
    }
    
    
    public void setDownloadURL2(String downloadURL2) {
        this.downloadURL2 = downloadURL2;
    }
    
    
    public String getDownloadURL3() {
        return downloadURL3;
    }
    
    
    public void setDownloadURL3(String downloadURL3) {
        this.downloadURL3 = downloadURL3;
    }
    
    
    public String getDownloadURL4() {
        return downloadURL4;
    }
    
    
    public void setDownloadURL4(String downloadURL4) {
        this.downloadURL4 = downloadURL4;
    }
    
    
    public String getDistributionPermissions() {
        return distributionPermissions;
    }
    
    
    public void setDistributionPermissions(String distributionPermissions) {
        this.distributionPermissions = distributionPermissions;
    }
    
    
    public String getEndUserLicenceAgreement() {
        return EndUserLicenceAgreement;
    }
    
    
    public void setEndUserLicenceAgreement(String endUserLicenceAgreement) {
        EndUserLicenceAgreement = endUserLicenceAgreement;
    }    
    
    private static String conv(long numPennies) {
        String s = String.valueOf(numPennies);
        if (numPennies == 0) return "0.00";
        if (s.length() <= 2) return "0." + numPennies;
        
        String pounds = s.substring(0, s.length()-2);
        String pennies = s.substring(s.length()-2);
        
        return pounds + "." + pennies;
    }

    public int getCostInPennies() {
        return costInPennies;
    }

    public void setCostInPennies(int costInPennies) {
        this.costInPennies = costInPennies;
    }     
}

