/*
 * Test.java
 *
 * Copyright (c) 2007  Vreixo Formoso
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See COPYING file for details.
 */

package org.pykix.libburnia.test;

import java.util.Arrays;

import org.pykix.libburnia.libburn.BlockType;
import org.pykix.libburnia.libburn.Burn;
import org.pykix.libburnia.libburn.BurnException;
import org.pykix.libburnia.libburn.BurnSource;
import org.pykix.libburnia.libburn.Disc;
import org.pykix.libburnia.libburn.DiscStatus;
import org.pykix.libburnia.libburn.Drive;
import org.pykix.libburnia.libburn.DriveInfo;
import org.pykix.libburnia.libburn.DriveStatus;
import org.pykix.libburnia.libburn.FormatDesc;
import org.pykix.libburnia.libburn.Formats;
import org.pykix.libburnia.libburn.Message;
import org.pykix.libburnia.libburn.MultiCaps;
import org.pykix.libburnia.libburn.Profile;
import org.pykix.libburnia.libburn.Progress;
import org.pykix.libburnia.libburn.ReadOpts;
import org.pykix.libburnia.libburn.ScsiAdr;
import org.pykix.libburnia.libburn.Session;
import org.pykix.libburnia.libburn.Source;
import org.pykix.libburnia.libburn.SpeedDescriptor;
import org.pykix.libburnia.libburn.Track;
import org.pykix.libburnia.libburn.WriteOpts;
import org.pykix.libburnia.libburn.WriteType;
import org.pykix.libburnia.libburn.Message.Severity;

public class Test {

	private static int blankDisc(Drive drive, boolean fast) 
	throws InterruptedException, BurnException {
		
		DiscStatus state = drive.getDiscStatus();
		Profile profile = drive.getProfile();
		
		System.out.println("Disc state: " + state);
		
		if (profile == Profile.DVD_RW_RESTRICTED_OVERWITE) {
			/* formatted DVD-RW will get blanked to sequential state */
		} else if (state == DiscStatus.BLANK) {
			System.err.println(
					"IDLE: Blank media detected. Will leave it untouched");
			return 2;
		} else if (state == DiscStatus.FULL ||
				state == DiscStatus.APPENDABLE) {
			/* this is what libburner is willing to blank */
		} else if (state == DiscStatus.EMPTY) {
			System.err.println("FATAL: No media detected in drive");
			return 0;
		} else {
			System.err.println("FATAL: Unsuitable drive and media state");
			return 0;
		}
		
		if( !drive.isErasable() ) {
			System.err.println("FATAL : Media is not of erasable type");
			return 0;
		}
		
		System.out.println("Beginning to " + (fast?"fast":"full") + "-blank media.");
		
		drive.erase(fast);

        Thread.sleep(1000);
		
        Progress p = new Progress();
        
        while ( drive.getDriveStatus(p) != DriveStatus.IDLE ) {
        	
        	System.out.printf("Blanking  ( %.1f%% done )\n", 
        			(float) p.getSector() * 100 / p.getSectors() );
            Thread.sleep(1000);
        }
        
        System.out.println("Done");
		return 1;
		
	}
	
	private static int writeImage(Drive drive, String path) 
	throws InterruptedException, BurnException {
		
		WriteOpts wo = drive.newWriteOpts();
		
		wo.setMulti(true);
		wo.setWriteType(WriteType.SAO, BlockType.MODE2_OK);
		
		BurnSource src = new Source(path, null);
		
		Disc disc = new Disc();
		Session s = new Session();
		Track t = new Track();
		t.setSource(src);
		s.addTrack(t, 0);
		disc.addSession(s, 0);
		
		wo.precheckWrite(disc, false);
		
		drive.write(wo, disc);

        Thread.sleep(1000);
		
        Progress p = new Progress();
        
        while ( drive.getDriveStatus(p) != DriveStatus.IDLE ) {
        	
        	System.out.printf("Blanking  ( %.1f%% done )\n", 
        			(float) p.getSector() * 100 / p.getSectors() );
            Thread.sleep(1000);
        }
        
        return 0;
	}
	
	public static void main(String[] args) throws BurnException, InterruptedException {

		Burn.initialize();
//		Message.setSeverities(Severity.ALL, Severity.ALL, "Test: ");
		
//		DriveInfo[] drives = Burn.scan();		
//		Drive drive = drives[0].getDrive();
		
//		drive.grab(true);
		
		System.out.println(Burn.convertFsAdr("/dev/sr0"));
		
		DriveInfo info = Burn.scanAndGrab("/dev/sg1", true);
		
		Drive drive = info.getDrive();
		
//		ScsiAdr ad = ScsiAdr.obtainScsiAdr(info.getAdr());
//		System.out.println(Burn.getVersion());
//		Burn.setSignalHandler( new Burn.SignalHandler() {
//			public int handleSignal(int signum, int flag) {
//				System.err.println("handled signal " + signum);
//				System.err.flush();
//				return -1;
//			}
//		}, 0);
		
		Formats fs = drive.getFormats();
		
		System.out.println("blsas = " + fs.getBlSas() + 
				", size = " + fs.getSize() +
				", status = " + fs.getStatus());
		
		FormatDesc fd = drive.getFormatDescr(0);
		
		System.out.println(" Type = " + fd.getType() + 
				", size = " + fd.getSize() );
		
//		WriteOpts wo = drive.newWriteOpts();
//		
//		System.out.println(drive.getAvailableSpace(wo));
		
//		drive.readAtip();
//		
//		MultiCaps mc = drive.getMultiCaps(WriteType.SAO);
//		
//		System.out.println( mc.getCurrentProfile() );
//		
//		mc = drive.getMultiCaps(WriteType.SAO);
//		System.out.println( mc.isWritingPossible() );
		
//		System.out.println( drive.getTrackNwa(null, 2) );
		
//		SpeedDescriptor[] sl = drive.getSpeedList();
//		
//		for ( SpeedDescriptor sd : sl ) {
//			System.out.println( sd.getWriteSpeed() +  " " + sd.getSource() );
//		}
//		
//		Message msg;
//		while ( (msg = Message.obtain(Severity.ALL)) != null ) {
//			System.out.println(msg.getMsgText());
//		}
		
//		blankDisc(drive, true);
//		writeImage(drive, "/home/metalpain/image.iso");
		
//		ReadOpts opts = drive.newReadOpts();
		
//		System.out.println( drive.getWriteSpeed() );

//		long i = 0;
//		while( i++ < 10000000000L) {
//			//System.out.print("aa");
//		}
		
		drive.release(false);
		
//		drives[0].forget(0);
		info.forget(0);

		Burn.finish();
	}
	
}
