package com.fer.labust.diverassistant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import com.fer.labust.diverassistant.MainActivity.MapSectionFragment;

import android.R.integer;
import android.widget.TextView;

public class DepthAssistant {

	private static final DepthAssistant instance = new DepthAssistant();

	private DepthAssistant() {
	}

	public static DepthAssistant getInstance() {
		return instance;
	}

	// private MapSectionFragment diveTimeLabel;
	private static final Map<Integer, String> maxDiveTime = new HashMap<Integer, String>();
	static {
		// <key-depth; value-mdt>
		maxDiveTime.put(12, "130 min");
		maxDiveTime.put(15, "80 min");
		maxDiveTime.put(18, "55 min");
		maxDiveTime.put(21, "45 min");
		maxDiveTime.put(24, "35 min");
		maxDiveTime.put(27, "25 min");
		maxDiveTime.put(30, "22 min");
		maxDiveTime.put(33, "15 min");
		maxDiveTime.put(36, "12 min");
		maxDiveTime.put(40, "8 min");
	}
	private static final Map<String, String> decompressionStops = new HashMap<String, String>();
	static {
		// <key - depth,time; value -
		// (durration1,durration2,durration3,durration4)>
		decompressionStops.put("18,70", "2");
		decompressionStops.put("18,80", "6");
		decompressionStops.put("18,90", "12");
		decompressionStops.put("21,50", "2");
		decompressionStops.put("21,55", "4");
		decompressionStops.put("21,60", "8");
		decompressionStops.put("21,65", "10");
		decompressionStops.put("21,70", "14");
		decompressionStops.put("21,75", "18");
		decompressionStops.put("24,30", "1");
		decompressionStops.put("24,40", "3");
		decompressionStops.put("24,45", "6");
		decompressionStops.put("24,50", "10");
		decompressionStops.put("24,55", "13");
		decompressionStops.put("24,60", "18");
		decompressionStops.put("24,65", "22,1");
		decompressionStops.put("24,70", "25,2");
		decompressionStops.put("24,75", "27,3");
		decompressionStops.put("27,25", "1");
		decompressionStops.put("27,30", "3");
		decompressionStops.put("27,35", "5");
		decompressionStops.put("27,40", "8");
		decompressionStops.put("27,45", "12,1");
		decompressionStops.put("27,50", "16,2");
		decompressionStops.put("27,55", "20,4");
		decompressionStops.put("27,60", "24,6");
		decompressionStops.put("30,20", "1");
		decompressionStops.put("30,25", "3");
		decompressionStops.put("30,30", "5,1");
		decompressionStops.put("30,35", "8,2");
		decompressionStops.put("30,40", "13,3");
		decompressionStops.put("30,45", "17,5");
		decompressionStops.put("30,50", "22,7");
		decompressionStops.put("30,55", "26,10");
		decompressionStops.put("30,60", "28,13,1");
		decompressionStops.put("33,20", "3");
		decompressionStops.put("33,25", "5,1");
		decompressionStops.put("33,30", "7,3");
		decompressionStops.put("33,35", "13,4");
		decompressionStops.put("33,40", "18,5,1");
		decompressionStops.put("33,45", "23,8,2");
		decompressionStops.put("33,50", "27,11,3");
		decompressionStops.put("36,10", "1");
		decompressionStops.put("36,15", "2");
		decompressionStops.put("36,20", "4,1");
		decompressionStops.put("36,25", "6,3");
		decompressionStops.put("36,30", "11,4,1");
		decompressionStops.put("36,35", "16,6,2");
		decompressionStops.put("36,40", "23,8,3");
		decompressionStops.put("36,45", "27,11,5");
		decompressionStops.put("39,10", "1");
		decompressionStops.put("39,15", "4");
		decompressionStops.put("39,20", "4,3");
		decompressionStops.put("39,25", "8,4,1");
		decompressionStops.put("39,30", "14,5,3");
		decompressionStops.put("39,35", "21,8,3,1");
		decompressionStops.put("39,40", "26,11,5,1");
		decompressionStops.put("42,10", "1");
		decompressionStops.put("42,15", "4,1");
		decompressionStops.put("42,20", "6,3,1");
		decompressionStops.put("42,25", "12,4,3");
		decompressionStops.put("42,30", "18,6,4,1");
		decompressionStops.put("42,35", "25,10,5,2");
		decompressionStops.put("45,6", "1");
		decompressionStops.put("45,9", "1");
		decompressionStops.put("45,12", "4");
		decompressionStops.put("45,15", "4,3");
		decompressionStops.put("45,18", "5,4,1");
		decompressionStops.put("45,21", "9,4,3");
		decompressionStops.put("45,24", "14,5,3,1");
		decompressionStops.put("45,27", "18,6,3,2");
		decompressionStops.put("45,30", "23,8,4,3");
		decompressionStops.put("48,6", "1");
		decompressionStops.put("48,9", "2");
		decompressionStops.put("48,12", "4,1");
		decompressionStops.put("48,15", "5,4");
		decompressionStops.put("48,18", "7,4,2");
		decompressionStops.put("48,21", "12,5,3,1");
		decompressionStops.put("48,24", "17,5,4,2");
		decompressionStops.put("48,27", "22,8,4,3");
		decompressionStops.put("51,6", "1");
		decompressionStops.put("51,9", "3");
		decompressionStops.put("51,12", "4,3");
		decompressionStops.put("51,15", "5,4,2");
		decompressionStops.put("51,18", "9,4,4");
		decompressionStops.put("51,21", "15,5,4,2");
	}
	private double depth;
	private int depthLevel = 0;

	// public DepthAssistant(MapSectionFragment txtView){
	// this.diveTimeLabel = txtView;
	// }

	public double getDepth() {
		return depth;
	}

	public double fetchDepth() {
		// TODO - fetch depth from sounder
		// this.depth = ----fetched
		// depth--------------------------------------------
		return 39;
	}

	public String calculateDivingDepth() {
		double currentDepth;
		currentDepth = fetchDepth();
		android.os.SystemClock.sleep(10000);
		while ((currentDepth - fetchDepth()) > 3
				|| (fetchDepth() - currentDepth) > 3) {
			currentDepth = fetchDepth();
			android.os.SystemClock.sleep(10000);
		}
		currentDepth = (currentDepth + fetchDepth()) / 2;
		if (currentDepth < 12)
			depthLevel = 12;
		else if (currentDepth < 15)
			depthLevel = 15;
		else if (currentDepth < 18)
			depthLevel = 18;
		else if (currentDepth < 21)
			depthLevel = 21;
		else if (currentDepth < 24)
			depthLevel = 24;
		else if (currentDepth < 27)
			depthLevel = 27;
		else if (currentDepth < 30)
			depthLevel = 30;
		else if (currentDepth < 33)
			depthLevel = 33;
		else if (currentDepth < 36)
			depthLevel = 36;
		else if (currentDepth < 40)
			depthLevel = 40;
		else if (currentDepth > 40)
			return "unknown";
		return maxDiveTime.get(depthLevel);
	}

	public int getDepthLevel() {
		return depthLevel;
	}

	public void setDepthLevel(int depthLevel) {
		this.depthLevel = depthLevel;
	}

	public String getDecoStops(String durration) {
		double depth = getDepth();
		depth = 39; // --------------------------------------------maknuti
		int dpt = (int) depth;
		if (dpt % 3 != 0) {
			dpt = dpt + (3 - dpt % 3);
		}
		String[] times = durration.split(":");
		int time = 0;
		if (times.length == 1) {
			time = 0;
		} else if (times.length == 2) {
			time = Integer.parseInt(times[0]);
		} else if (times.length == 3) {
			time = Integer.parseInt(times[0]) * 60;
			time = time + Integer.parseInt(times[1]);
		}

		time=35; //-------------------------------------------------------------maknuti
		if (dpt <= 42) {
			if (time % 5 != 0) {
				time = time + (5 - time % 5);
			}
		} else {
			if (time % 3 != 0) {
				time = time + (3 - time % 3);
			}
		}
		String key = dpt + "," + time;
		return decompressionStops.get(key);
	}

}
