package com.javarush.task.task20.task2027;

import java.util.ArrayList;
import java.util.List;

/*
Кроссворд
*/
public class Solution {

    public static void main(String[] args) {
        int[][] crossword = new int[][]{
                {'f', 'd', 'e', 'r', 'l', 'k'},
                {'u', 's', 'a', 'm', 'e', 'o'},
                {'l', 'n', 'g', 'r', 'o', 'v'},
                {'m', 'l', 'p', 'r', 'r', 'h'},
                {'p', 'o', 'e', 'e', 'j', 'j'}
        };
        //      detectAllWords(crossword, "home", "same");
        for (Word word : detectAllWords(crossword, "red","rsa","kovh")) {
            System.out.println(word);
        }
        /*
Ожидаемый результат
red - (3, 0) - (1, 0)
kovh - (5, 0) - (5, 3)
         */
    }

    public static List<Word> detectAllWords(int[][] crossword, String... words) {
        List<Word> wordList = new ArrayList<>();
        String[] searchWords = words;
        int[][] vectors = {{-1, -1}, {1, 0}, {1, 1}, {-1, 0}, {-1, 1}, {0, 1}, {0, 0}, {0, -1}};
        boolean endWord = false;
        int beginX, beginY, endX, endY;
        for (String searchWord : searchWords) {
            char tempChar = searchWord.charAt(0);
            lab:
            for (int i = 0; i < crossword.length; i++) {
                for (int j = 0; j < crossword[0].length; j++) {
                    if ((char) crossword[i][j] == tempChar) {
                        beginX = j;
                        beginY = i;

                        for (int l = 0; l < vectors.length; l++) {
                            int k = 0;
                            int vectorI = vectors[l][0];
                            int vectorJ = vectors[l][1];


                            while (k < searchWord.length() - 1) {
                                if (k == searchWord.length() - 2) endWord = true;
                                tempChar = searchWord.charAt(++k);

                                try {

                                    if (crossword[i + vectorI][j + vectorJ] == tempChar & i < crossword.length & !endWord) {
                                        vectorI += vectors[l][0];
                                        vectorJ += vectors[l][1];
                                        continue;
                                    } else if (endWord & crossword[i + vectorI][j + vectorJ] == tempChar) {
                                        endX = j + vectorJ;
                                        endY = i + vectorI;
                                        Word word = new Word(searchWord);
                                        word.setStartPoint(beginX, beginY);
                                        word.setEndPoint(endX, endY);
                                        wordList.add(word);
                                        endWord = false;
                                        break lab;
                                    } else if (endWord & crossword[i + vectorI][j + vectorJ] != tempChar & searchWord.length() > 2) {
                                        endWord = false;
                                        break;
                                    } else break;
                                } catch (Exception e) {
                                    break;
                                }

                            }
                        }
                    }

                }

            }

        }

        return wordList;
    }

    public static class Word {
        private String text;
        private int startX;
        private int startY;
        private int endX;
        private int endY;

        public Word(String text) {
            this.text = text;
        }

        public void setStartPoint(int i, int j) {
            startX = i;
            startY = j;
        }

        public void setEndPoint(int i, int j) {
            endX = i;
            endY = j;
        }

        @Override
        public String toString() {
            return String.format("%s - (%d, %d) - (%d, %d)", text, startX, startY, endX, endY);
        }
    }
}