package turgen.generator;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.Serializable;
import java.util.StringTokenizer;
import turgen.FileFormatException;

public class PulseCrate implements Serializable {

    public int pilotTonePulseWidth=0;
    public int longPulseWidth=0;
    public int shortPulseWidth=0;
    public int syncPulseWidth=0;
    public int stopPulseWidth=0;
    public int baudRate=0;
    public String name="";
    
    private boolean immediateCorrectionsApplied=false;
    private boolean permanentCorrectionsApplied=false;

    public String toString() {
        return name;
    }

    public PulseCrate getCopy() {
        PulseCrate c = new PulseCrate();
        c.pilotTonePulseWidth=pilotTonePulseWidth;
        c.longPulseWidth=longPulseWidth;
        c.shortPulseWidth=shortPulseWidth;
        c.syncPulseWidth=syncPulseWidth;
        c.stopPulseWidth=stopPulseWidth;
        c.baudRate=baudRate;
        c.name=name;
        return c;
    }
    
    /** Apply fine tuning PulseCrate
     * 
     * @param fc Correction PulseCrate
     */
    public void applyCorrections(PulseCrate fc,boolean immediate) {
        
        if (immediate==false && immediateCorrectionsApplied==true) return;
        if (immediate==true  && permanentCorrectionsApplied==true) return;
        
        if (immediate==true) {
            immediateCorrectionsApplied=true;
        }
        else {
            permanentCorrectionsApplied=true;
        }
        
        /*Debug*/
        /*System.out.format("Pulse corrections applied: %d,%d,%d,%d,%d\n",
                fc.pilotTonePulseWidth,
                fc.longPulseWidth,
                fc.shortPulseWidth,
                fc.syncPulseWidth,
                fc.stopPulseWidth
                );*/
        
        pilotTonePulseWidth +=fc.pilotTonePulseWidth;
        longPulseWidth += fc.longPulseWidth;
        shortPulseWidth += fc.shortPulseWidth;
        syncPulseWidth += fc.syncPulseWidth;
        stopPulseWidth += fc.stopPulseWidth;
        
        if (pilotTonePulseWidth<2) pilotTonePulseWidth=2;
        if (longPulseWidth<2) longPulseWidth=2;
        if (shortPulseWidth<2) shortPulseWidth=2;
        if (syncPulseWidth<2) syncPulseWidth=2;
        if (stopPulseWidth<2) stopPulseWidth=2;
    }
    
    public void saveToStream(DataOutputStream dos) throws Exception {
        dos.writeUTF("P@");
        
        dos.writeUTF(name);
        dos.writeInt(baudRate);
        
        dos.writeInt(pilotTonePulseWidth);
        dos.writeInt(longPulseWidth);
        dos.writeInt(shortPulseWidth);
        dos.writeInt(syncPulseWidth);
        dos.writeInt(stopPulseWidth);
    }
    
    public void loadFromStream(DataInputStream dis) throws Exception {
        String eyeCatcher = dis.readUTF();
        if (!eyeCatcher.equals("P@")) {
            throw new FileFormatException("Pulses corrupt. 'P@' header of serialized pulses not found.");
        }
        name=dis.readUTF();
        baudRate=dis.readInt();
        
        pilotTonePulseWidth=dis.readInt();
        longPulseWidth=dis.readInt();
        shortPulseWidth=dis.readInt();
        syncPulseWidth=dis.readInt();
        stopPulseWidth=dis.readInt();
    }
    
    public String toPropertyValueString() {
        StringBuilder sb = new StringBuilder();
        sb.append(pilotTonePulseWidth);
        sb.append('%');
        sb.append(longPulseWidth);
        sb.append('%');
        sb.append(shortPulseWidth);
        sb.append('%');
        sb.append(syncPulseWidth);
        sb.append('%');
        sb.append(stopPulseWidth);
        
        return sb.toString();
    }
    
    public void readFromPropertyValueString(String propertyValue) {
        
        StringTokenizer tk = new StringTokenizer(propertyValue,"%");
        
        try {
            if (tk.hasMoreTokens()) {
                this.pilotTonePulseWidth=Integer.parseInt(tk.nextToken());
            }
            if (tk.hasMoreTokens()) {
                this.longPulseWidth=Integer.parseInt(tk.nextToken());
            }
            if (tk.hasMoreTokens()) {
                this.shortPulseWidth=Integer.parseInt(tk.nextToken());
            }
            if (tk.hasMoreTokens()) {
                this.syncPulseWidth=Integer.parseInt(tk.nextToken());
            }
            if (tk.hasMoreTokens()) {
                this.stopPulseWidth=Integer.parseInt(tk.nextToken());
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        
    }
    
    
}
