package com.tcl.factory.view;

import java.io.File;

import com.tcl.factory.thirdparty.ThirdParty;
import com.tcl.factory.utils.Debug;

//import android.app.AlertDialog;
//import android.content.DialogInterface;
import android.view.KeyEvent;
//import android.tclwidget.TCLAlertDialog;
//import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import com.tcl.factory.bsp.TCL;
 
public class DModeRecordMenu extends SubMenu
{
	private final static String TAG = "DModeRecordMenu";
	public static  String MenuInfos_DModeRecordMenu[][]={
		{"Record TS","Start"},
	};	
	private Context mContext;
	private boolean RecordingStatu = false;
	
	public DModeRecordMenu(Context context)
	{
		mContext = context;
		super.menuInfos=MenuInfos_DModeRecordMenu;
		super.menuLabel = "Record Menu";		
		super.setTAG();
		updateMenuInfos();
	}

	public void KeyEvent(int keyCode,int pos)
	{
		if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT||keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
			if(RecordingStatu==false){	//start to record TS
				if(startRecordTS()){
					RecordingStatu = true;
					Toast.makeText(mContext, "Recording TS...", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(mContext, "Recording TS fail!", Toast.LENGTH_SHORT).show();
				}
			}else{//stop recording
				ThirdParty.stopRecordTS();
				RecordingStatu = false;
				/*
				if(ThirdParty.stopRecordTS()){
					RecordingStatu = false;
					Toast.makeText(mContext, "save file success!", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(mContext, "save file fail!", Toast.LENGTH_SHORT).show();
				}	
				*/			
			}
		}
	}

	@Override
	public void updateMenuInfos() {
		//recordDialogShow();
		if(RecordingStatu){
			menuInfos[0][1]="stop"; 
		}else{
			menuInfos[0][1]="start"; 
		}
	}

    public boolean fileIsExists(String strFile)  
    {  
        try  
        {  
            File f=new File(strFile);  
            if(!f.exists())  
            {  
            	return false;  
            }    
        }  
        catch (Exception e)  
        {  
            return false;  
        }
        return true;  
    }  
	
	private boolean startRecordTS() {
		String path = TCL.getUSBMountPath(mContext);
		if(null==path || ("".equals(path)) ){
			Toast.makeText(mContext, "No found USB!!", Toast.LENGTH_SHORT).show();
			return false;
		} else {
			String filePath = path+"/Record_1.ts";
			for(int i=0;i<255;i++){
				filePath = path+"/Record_"+i+".ts";
				if(!fileIsExists(filePath)){
					break;
				}
			}
			Debug.v(TAG, "filePath="+filePath);
			if(!ThirdParty.startRecordTS(filePath)){
				return false;
			}
		}
		return true;
	}		
/*	
	private void recordDialogShow() {
		Debug.v(TAG, "recordDialogShow start");
		final EditText inputServer = new EditText(mContext);
		inputServer.setFocusable(true);
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle("Record TS menu").setMessage("Please input file name:").setView(inputServer);
		builder.setPositiveButton("Start",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				String inputName = inputServer.getText().toString();
				if(ThirdParty.startRecordTS(inputName)){
					Toast.makeText(mContext, "Recording TS...", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(mContext, "Recording TS fail!", Toast.LENGTH_SHORT).show();
				}
			}
		});		
		builder.setNegativeButton("Stop",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if(ThirdParty.stopRecordTS()){
					Toast.makeText(mContext, "save file success!", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(mContext, "save file fail!", Toast.LENGTH_SHORT).show();
				}
			}
		});
		Debug.v(TAG, "recordDialogShow end");
		builder.show();
	}
*/
}
