/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package baseclasses;

import baseclasses.Triangle;
import baseclasses.Point;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

/**
 *
 * @author Alex
 */
public class ObjExport {
    
    /**
     * Filename to write to.
     */
    String fileName = "";
    
    /**
     * Vertices that will be written.
     */
    Point[] vertices = null;
    
    /**
     * Faces that will be written.
     */
    Triangle[] faces = null;
    
    /**
     * Output writer.
     */
    private BufferedWriter output;
    
    /**
     * Output directory.
     */
    private final String outputDir = "./Output/";
    
    /**
     * ObjOutput constructor. 
     * @param file file name to write to.
     * @param v vertices.
     * @param f faces.
     */
    public ObjExport(String file, Point[] v, Triangle[] f) {
        this.fileName = file;
        this.vertices = v;
        this.faces = f;
    } 
    
    /**
     * Write new .obj file.
     * @throws IOException if bad file.
     */
    public void write() throws IOException {
        System.out.println("Exporting: " + this.fileName);
        this.output = new BufferedWriter(new FileWriter(this.outputDir 
                + this.fileName));
        this.output.write(this.header());
        
        for (int i = 0; i < this.vertices.length; i++) {
        //for (int i = 0; i < 10; i++) {
            this.output.write(this.vertexStGen(this.vertices[i]));
        }
        this.output.write("# " + this.vertices.length + " vertices\n\n");
        
        if (this.faces != null) {
            for (int i = 0; i < this.faces.length; i++) {
            //for (int i = 0; i < 10; i++) {
                this.output.write(this.faceStGen(this.faces[i]));
            }
            this.output.write("# " + this.faces.length + " faces\n\n");

            this.output.write("# End of File\n\n");
        }
        System.out.println("File export complete!");
        this.output.close();
    }
    
    /**
     * Generate string for triangle. 
     * @param out Triangle to generate line for.
     * @return a String formatted for  output.
     */
    private String faceStGen(Triangle out) {
        String ou = "";
        int[] ind = out.getVertIndices();
        for (int i = 0; i < ind.length; i++) {
            ind[i] += 1;
        }
        ou += "f " + ind[0] + " " + ind[1] + " " + ind[2];
        ou += "\n";
        return ou;
    }
    
    /**
     * Generate string for vertex. 
     * @param out Point to generate line for.
     * @return a String formatted for  output.
     */
    private String vertexStGen(Point out) {
        String ou = "";
        double[] coor = out.getCoor();
        ou += "v " + coor[0] + " " + coor[1] + " " + coor[2]; 
        double[] rgb = out.getRGBColor();
        ou += " " + rgb[0] + " " + rgb[1] + " " + rgb[2];
        ou += "\n";
        return ou;
    }
    
    /**
     * Generate header for .obj File.
     * @return a string with header information.
     */
    private String header() {
        String head = "";
        head += "#######\n";
        Date date = new Date();
        head += "# Mesh Segmentation of Defect\n";
        head += "# CIS2 Spring 2015\n#\n";
        head += "# Output generated at: \n";
        head += "# " + date.toString() + "\n#\n";
        head += "# Vertices: " + this.vertices.length + "\n";
        if (this.faces != null) {
            head += "# Faces: " + this.faces.length + "\n";
        }
        head += "#######\n";
        return head;
    }
    
}
