package com.tsi.navi.util;

import java.util.concurrent.ConcurrentHashMap;

public class TsiDirection {

    private ConcurrentHashMap<Integer, String> directionMap = new ConcurrentHashMap<Integer, String>();
    private ConcurrentHashMap<String, Integer> directionNameMap = new ConcurrentHashMap<String, Integer>();

    private ConcurrentHashMap<Integer, Integer> signalDirectionMap = new ConcurrentHashMap<Integer, Integer>();
    private ConcurrentHashMap<Integer, Integer> runDirectionMap = new ConcurrentHashMap<Integer, Integer>();

    public TsiDirection() {
        this.directionMap.put(10, "북(10)");
        this.directionMap.put(20, "동(20)");
        this.directionMap.put(30, "남(30)");
        this.directionMap.put(40, "서(40)");
        this.directionMap.put(50, "북동(50)");
        this.directionMap.put(60, "남동(60)");
        this.directionMap.put(70, "남서(70)");
        this.directionMap.put(80, "북서(80)");

        this.directionNameMap.put("북(10)", 10);
        this.directionNameMap.put("동(20)", 20);
        this.directionNameMap.put("남(30)", 30);
        this.directionNameMap.put("서(40)", 40);
        this.directionNameMap.put("북동(50)", 50);
        this.directionNameMap.put("남동(60)", 60);
        this.directionNameMap.put("남서(70)", 70);
        this.directionNameMap.put("북서(80)", 80);

        int directionCode = 0;
        for (int ii = 0; ii <= 360; ii++) {
            if      (ii >  22 && ii <=  67) directionCode = 70;
            else if (ii >  67 && ii <= 112) directionCode = 40;
            else if (ii > 112 && ii <= 157) directionCode = 80;
            else if (ii > 157 && ii <= 202) directionCode = 10;
            else if (ii > 202 && ii <= 247) directionCode = 50;
            else if (ii > 247 && ii <= 292) directionCode = 20;
            else if (ii > 292 && ii <= 337) directionCode = 60;
            else directionCode = 30;

            this.signalDirectionMap.put(ii, directionCode);
        }

        for (int ii = 0; ii <= 360; ii++) {
            if      (ii >=  22 && ii <  67) directionCode = 50;
            else if (ii >=  67 && ii < 112) directionCode = 20;
            else if (ii >= 112 && ii < 157) directionCode = 60;
            else if (ii >= 157 && ii < 202) directionCode = 30;
            else if (ii >= 202 && ii < 247) directionCode = 70;
            else if (ii >= 247 && ii < 292) directionCode = 40;
            else if (ii >= 292 && ii < 337) directionCode = 80;
            else directionCode = 10;

            this.runDirectionMap.put(ii, directionCode);
        }

    }
    /*
    public static int getDirectionCode(double bearing) {

        if (bearing >= 22.5 && bearing < 67.5) return 70;
        if (bearing >= 67.5 && bearing < 112.5) return 40;
        if (bearing >= 112.5 && bearing < 157.5) return 80;
        if (bearing >= 157.5 && bearing < 202.5) return 10;
        if (bearing >= 202.5 && bearing < 247.5) return 50;
        if (bearing >= 247.5 && bearing < 292.5) return 20;
        if (bearing >= 292.5 && bearing < 337.5) return 60;
        return 30;
    }

    public static int getRunDirectionCode(double bearing) {

        if (bearing >= 22.5 && bearing < 67.5) return 50;
        if (bearing >= 67.5 && bearing < 112.5) return 20;
        if (bearing >= 112.5 && bearing < 157.5) return 60;
        if (bearing >= 157.5 && bearing < 202.5) return 30;
        if (bearing >= 202.5 && bearing < 247.5) return 70;
        if (bearing >= 247.5 && bearing < 292.5) return 40;
        if (bearing >= 292.5 && bearing < 337.5) return 80;
        return 10;
    }
    */

    public int getNameCode(String directionName) {
        Integer direction = this.directionNameMap.get(directionName);
        return (direction != null) ? direction : 99;
    }

    public String getName(int direction) {
        String directionName = this.directionMap.get(direction);
        return (directionName != null) ? directionName :  "(" + direction + ")";
    }

    public int getSignalDirectionCode(double bearing) {
        if (bearing < 0) bearing = (360 + bearing);
        Integer directionCode = this.signalDirectionMap.get((int)bearing);
        return (directionCode != null) ? directionCode :  0;
    }

    public int getRunDirectionCode(double bearing) {
        if (bearing < 0) bearing = (360 + bearing);
        Integer directionCode = this.runDirectionMap.get((int)bearing);
        return (directionCode != null) ? directionCode :  0;
    }

    public int getSignal2RunDirection(int signalDirection) {
        if (signalDirection == 70) return 50;
        if (signalDirection == 40) return 20;
        if (signalDirection == 80) return 60;
        if (signalDirection == 10) return 30;
        if (signalDirection == 50) return 70;
        if (signalDirection == 20) return 40;
        if (signalDirection == 60) return 80;
        if (signalDirection == 30) return 10;
        return 0;
    }
}
