%function missions = splitMission(mission,n)
%  Splits missions on ascends
function missions = splitMission(mission,n)

missions = [];
if ~checkMissionFile(mission), return, end

if n <= 1, return, end

index = findin(mission,'name','missiontimer');
if ~isfield(mission{index},'timeout'),
	uiwait(msgbox('Mission timer missing timeout','modal'))
	return
end

timeout = getfield(mission{index},'timeout');
if isempty(timeout),
	uiwait(msgbox('Mission timer missing timeout','modal'))
	return
end

% get splits
[splits,splittime,n] = getSplits(mission,timeout,n);
if isempty(splits), return, end

% split at GPS fixes
splits = adjustSplits(mission,splits);
if isempty(splits), return, end

% grab the control sonar on, if there is one
ii = findin(mission,'name','controlsonar',1);
if ii > 0, sonarOn = mission{ii};
else sonarOn = []; end

% insert transit?
button = questdlg('Insert a transit at the start of each mission?');

% create new missions
header = mission(1:max(splits(1)-1,1));
for ii=1:length(splits)-1,
	tmpheader = header;
	index = findin(tmpheader,'name','missiontimer');
	tmpheader{index}.timeout = approximate(splittime);
	tmpheader{1}.name = setName(mission{1}.name,n-ii+1);
	missions{ii} = [tmpheader mission(splits(ii):splits(ii+1))];
	
	% add a transit
	if strcmp(button,'Yes'),	
	
		% trim off the last getGps and take the previous waypoint
		missions{ii} = missions{ii}(1:length(missions{ii})-1);
		wpt = missions{ii}{length(missions{ii})};

		% create transit to start and turn on sonar
		if ~isempty(findstr(wpt.name,'waypoint')), 
			transit = [];
			transit{1} = getgps;
			transit{2} = wpt;
			transit{2}.depth = 3;
			transit{2}.duration = 600;	
			transit{3} = ascend;
			transit{4} = getgps;
			if ~isempty(sonarOn), transit{5} = sonarOn; end

			missions{ii} = [missions{ii} fliplr(transit)];
		else
			errorbox('Could not find waypoint to insert transit.  Skipping...')
		end
	
	% otherwise just toggle the sonar if we need to
	else
		toggleSonar{1} = sonarOn;
		missions{ii} = [missions{ii} toggleSonar];
	end
	
end
tmpheader = header;
index = findin(tmpheader,'name','missiontimer');
tmpheader{index}.timeout = approximate(timeout - (n-1)*splittime);
tmpheader{1}.name = setName(mission{1}.name,1);
if ~isempty(sonarOn), tmpheader{end+1} = controlsonar('lowpower'); end
missions{ii+1} = [tmpheader mission(splits(ii+1):length(mission))];


%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function name = setName(name,index)
[t,r] = strtok(name,'.');
name = [t int2str(index) r];


%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function splits = adjustSplits(mission,splits)
for ii=2:length(splits),
	index = splits(ii);
	while ~strcmp(mission{index}.name,'ascend'),
		index = index - 1;
		if index < splits(ii-1),
			uiwait(msgbox('Not enough Ascends to split mission','modal'))
			splits = [];
			return
		end
	end
	splits(ii) = index - 1;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [splits,splittime,n] = getSplits(mission,timeout,n)
[splittime,n] = getSplitTime(timeout,n);
if isempty(splittime), splits = []; return, end
index = getStartIndex(mission);
time = 0;
splits(1) = index;
for ii = 1:n-1,
	while time < ii*splittime,
		if isfield(mission{index},'duration'),
			time = time + getfield(mission{index},'duration');
		end
		index = index+1;
	end
	index = index - 1;
	splits = [splits; index];
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [splittime,n] = getSplitTime(timeout,n)
minMissionDuration = 60*60;  %seconds
startn = n;
while 1,
	splittime = timeout/n;
	if splittime < minMissionDuration,
		n = n-1;
		if n <= 1,
			uiwait(msgbox('Mission is too short to split','modal'))
			splittime = [];
			return
		end
	else, break, end
end

if n ~= startn,
	uiwait(msgbox(['Minimum split mission time is ' ...
		mynum2str(minMissionDuration/3600) ' hours.' ...
		'  Using n = ' int2str(n) ' instead.' ]))
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function index = getStartIndex(mission)
for index=2:length(mission),
	if ~strcmp(mission{index}.name,'depthenvelope') & ...
		~strcmp(mission{index}.name,'missiontimer'),
		break
	end
end