package cokr.xit.problems;

import org.junit.jupiter.api.Test;

import java.util.*;

class Solution1 {


    @Test
    public void main(){
        String path = null;

        path = "EEESEEEEEENNNN";
        solution(path);
//        System.out.println(solution(path));
        path = "SSSSSSWWWNNNNNN";
        solution(path);
//        System.out.println(solution(path));


    }


    public String[] solution(String path){
        String[] answer = {};

        Map<String,Map<String, String>> mDirection = createLeftRigthByDirection();

        List<Map<String, Object>> infos = new ArrayList<>();
        Map<String, Object> info = new HashMap<>();


        String curD = null;
        int m = 0;
        for(int i=0; i<path.length(); i++) {
            String chngD = path.substring(i, i + 1);
            if(i==0) curD = chngD;
            else{
                if(curD.equals(chngD)){
                    m++;
                }else{
                    info.put("meter", m+1);
                    info.put("direction", mDirection.get(curD).get(chngD));
                    info.put("time", i+1);
                    infos.add(info);
                    curD = chngD;
                    m = 0;
                    info = new HashMap<>();
                }
            }
        }


        String msgTmplt = "Time %d: Go straight %dm and turn %s";


        List<String> messages = new ArrayList<>();
        for(int i=0; i<infos.size(); i++){
            Map<String, Object> inf = infos.get(i);
            int x = (int) inf.get("time");
            int y = (int) inf.get("meter");
            String direction = (String) inf.get("direction");
            if(i==0 && y <= 5)
                messages.add(String.format(msgTmplt, 0, (y*100), direction));
            else
                if(y > 5)
                    messages.add(String.format(msgTmplt, x-5, 500, direction));
                else
                    messages.add(String.format(msgTmplt, x-1, (y*100), direction));
        }
        System.out.println(messages.toString());
        answer = new String[messages.size()];
        for(int i=0; i<answer.length; i++)
            answer[i] = messages.get(i);
//        answer = massages.toArray();
        return answer;
    }

    private Map<String, Map<String, String>> createLeftRigthByDirection(){

        Map<String,String> lrE = new HashMap<>();
        lrE.put("N", "left");
        lrE.put("S", "right");
        Map<String,String> lrW = new HashMap<>();
        lrW.put("S", "left");
        lrW.put("N", "right");
        Map<String,String> lrS = new HashMap<>();
        lrS.put("E", "left");
        lrS.put("W", "right");
        Map<String,String> lrN = new HashMap<>();
        lrN.put("W", "left");
        lrN.put("E", "right");
        Map<String,Map<String, String>> mDirection = new HashMap<>();
        mDirection.put("E", lrE);
        mDirection.put("W", lrW);
        mDirection.put("S", lrS);
        mDirection.put("N", lrN);
        return mDirection;
    }



}