package com.reallinkbilling.utils;

/**
 * Utility to convert Unicode/Nirmala Devanagari text to KrutiDev encoded text.
 *
 * The implementation ports the provided JavaScript logic and mappings
 * (array_one → array_two), including repositioning of the "ि" matra and
 * handling of leading half "र्".
 */
public final class UnicodeToKrutiDev {

    private static final String[] ARRAY_ONE = new String[] {
            "‘",
            "’",
            "“",
            "”",
            "(",
            ")",
            "{",
            "}",
            "=",
            "।",
            "?",
            "-",
            "µ",
            "॰",
            ",",
            ".",
            "् ",
            "०",
            "१",
            "२",
            "३",
            "४",
            "५",
            "६",
            "७",
            "८",
            "९",
            "x",
            "फ़्",
            "क़",
            "ख़",
            "ग़",
            "ज़्",
            "ज़",
            "ड़",
            "ढ़",
            "फ़",
            "य़",
            "ऱ",
            "ऩ",
            "त्त्",
            "त्त",
            "क्त",
            "दृ",
            "कृ",
            "ह्न",
            "ह्य",
            "हृ",
            "ह्म",
            "ह्र",
            "ह्",
            "द्द",
            "क्ष्",
            "क्ष",
            "त्र्",
            "त्र",
            "ज्ञ",
            "छ्य",
            "ट्य",
            "ठ्य",
            "ड्य",
            "ढ्य",
            "द्य",
            "द्व",
            "श्र",
            "ट्र",
            "ड्र",
            "ढ्र",
            "छ्र",
            "क्र",
            "फ्र",
            "द्र",
            "प्र",
            "ग्र",
            "रु",
            "रू",
            "्र",
            "ओ",
            "औ",
            "आ",
            "अ",
            "ई",
            "इ",
            "उ",
            "ऊ",
            "ऐ",
            "ए",
            "ऋ",
            "क्",
            "क",
            "क्क",
            "ख्",
            "ख",
            "ग्",
            "ग",
            "घ्",
            "घ",
            "ङ",
            "चै",
            "च्",
            "च",
            "छ",
            "ज्",
            "ज",
            "झ्",
            "झ",
            "ञ",
            "ट्ट",
            "ट्ठ",
            "ट",
            "ठ",
            "ड्ड",
            "ड्ढ",
            "ड",
            "ढ",
            "ण्",
            "ण",
            "त्",
            "त",
            "थ्",
            "थ",
            "द्ध",
            "द",
            "ध्",
            "ध",
            "न्",
            "न",
            "प्",
            "प",
            "फ्",
            "फ",
            "ब्",
            "ब",
            "भ्",
            "भ",
            "म्",
            "म",
            "य्",
            "य",
            "र",
            "ल्",
            "ल",
            "ळ",
            "व्",
            "व",
            "श्",
            "श",
            "ष्",
            "ष",
            "स्",
            "स",
            "ह",
            "ऑ",
            "ॉ",
            "ो",
            "ौ",
            "ा",
            "ी",
            "ु",
            "ू",
            "ृ",
            "े",
            "ै",
            "ं",
            "ँ",
            "ः",
            "ॅ",
            "ऽ",
            "् ",
            "्"
    };

    private static final String[] ARRAY_TWO = new String[] {
            "^",
            "*",
            "Þ",
            "ß",
            "¼",
            "½",
            "¿",
            "À",
            "¾",
            "A",
            "\\",
            "&",
            "&",
            "Œ",
            "]",
            "-",
            "~ ",
            "å",
            "ƒ",
            "„",
            "…",
            "†",
            "‡",
            "ˆ",
            "‰",
            "Š",
            "‹",
            "Û",
            "¶",
            "d",
            "[k",
            "x",
            "T",
            "t",
            "M+",
            "<+",
            "Q",
            ";",
            "j",
            "u",
            "Ù",
            "Ùk",
            "Dr",
            "–",
            "—",
            "à",
            "á",
            "â",
            "ã",
            "ºz",
            "º",
            "í",
            "{",
            "{k",
            "«",
            "=",
            "K",
            "Nî",
            "Vî",
            "Bî",
            "Mî",
            "<î",
            "|",
            "}",
            "J",
            "Vª",
            "Mª",
            "<ªª",
            "Nª",
            "Ø",
            "Ý",
            "æ",
            "ç",
            "xz",
            "#",
            ":",
            "z",
            "vks",
            "vkS",
            "vk",
            "v",
            "bZ",
            "b",
            "m",
            "Å",
            ",s",
            ",",
            "_",
            "D",
            "d",
            "ô",
            "[",
            "[k",
            "X",
            "x",
            "?",
            "?k",
            "³",
            "pkS",
            "P",
            "p",
            "N",
            "T",
            "t",
            "÷",
            ">",
            "¥",
            "ê",
            "ë",
            "V",
            "B",
            "ì",
            "ï",
            "M",
            "<",
            ".",
            ".k",
            "R",
            "r",
            "F",
            "Fk",
            ")",
            "n",
            "/",
            "/k",
            "U",
            "u",
            "I",
            "i",
            "¶",
            "Q",
            "C",
            "c",
            "H",
            "Hk",
            "E",
            "e",
            "¸",
            ";",
            "j",
            "Y",
            "y",
            "G",
            "O",
            "o",
            "'",
            "'k",
            "\"",
            "\"k",
            "L",
            "l",
            "g",
            "v‚",
            "‚",
            "ks",
            "kS",
            "k",
            "h",
            "q",
            "w",
            "`",
            "s",
            "S",
            "a",
            "¡",
            "%",
            "W",
            "·",
            "~ ",
            "~"
    };

    private UnicodeToKrutiDev() {}

    public static String convert(String unicodeText) {
        if (unicodeText == null || unicodeText.isEmpty()) {
            return unicodeText;
        }

        String modified = unicodeText;

        // Alternate single quotes to ^ and * from left to right
        int posQuote = modified.indexOf('\'');
        while (posQuote >= 0) {
            modified = replaceFirstOccurrence(modified, "'", "^");
            modified = replaceFirstOccurrence(modified, "'", "*");
            posQuote = modified.indexOf('\'');
        }

        // Alternate double quotes to ß and Þ from left to right
        int posDq = modified.indexOf('"');
        while (posDq >= 0) {
            modified = replaceFirstOccurrence(modified, "\"", "ß");
            modified = replaceFirstOccurrence(modified, "\"", "Þ");
            posDq = modified.indexOf('"');
        }

        // Normalize two-byte nukta characters to single-byte forms
        modified = modified.replace("क़", "क़");
        modified = modified.replace("ख़", "ख़");
        modified = modified.replace("ग़", "ग़");
        modified = modified.replace("ज़", "ज़");
        modified = modified.replace("ड़", "ड़");
        modified = modified.replace("ढ़", "ढ़");
        modified = modified.replace("ऩ", "ऩ");
        modified = modified.replace("फ़", "फ़");
        modified = modified.replace("य़", "य़");
        modified = modified.replace("ऱ", "ऱ");

        // Reposition 'ि' matra as 'f' before the consonant cluster
        int posF = modified.indexOf('ि');
        while (posF != -1) {
            if (posF > 0) {
                String charLeft = modified.substring(posF - 1, posF);
                modified = modified.replace(charLeft + "ि", "f" + charLeft);
                posF = posF - 1;

                while (posF > 0 && modified.charAt(posF - 1) == '्') {
                    String toReplace = modified.substring(posF - 2, posF);
                    modified = modified.replace(toReplace + "f", "f" + toReplace);
                    posF = posF - 2;
                }
            }
            posF = modified.indexOf('ि', posF + 1);
        }

        // Handle leading half 'र्' by placing 'Z' after the next consonant and its matras
        final String setOfMatras = "ािीुूृेैोौं:ँॅ";
        modified = modified + "  ";
        int posHalfR = modified.indexOf("र्");
        while (posHalfR > 0) {
            int zPos = posHalfR + 2; // position after 'र्'
            int lookAheadIndex = zPos + 1;
            while (lookAheadIndex < modified.length() && setOfMatras.indexOf(modified.charAt(lookAheadIndex)) != -1) {
                zPos++;
                lookAheadIndex = zPos + 1;
            }

            String before = modified.substring(0, posHalfR);
            String middle = modified.substring(posHalfR + 2, zPos + 1);
            String after = modified.substring(zPos + 1);
            modified = before + middle + "Z" + after;

            posHalfR = modified.indexOf("र्");
        }
        modified = modified.substring(0, modified.length() - 2);

        // Substitute array_two elements in place of corresponding array_one elements
        for (int i = 0; i < ARRAY_ONE.length; i++) {
            String find = ARRAY_ONE[i];
            String replace = ARRAY_TWO[i];
            int idx = modified.indexOf(find);
            while (idx != -1) {
                modified = replaceFirstOccurrence(modified, find, replace);
                idx = modified.indexOf(find);
            }
        }

        return modified;
    }

    private static String replaceFirstOccurrence(String source, String find, String replace) {
        int p = source.indexOf(find);
        if (p < 0) return source;
        return source.substring(0, p) + replace + source.substring(p + find.length());
    }

    public static void main(String[] args) {
        String input = "सोलापूर";
        String output = convert(input);
        System.out.println("Input: " + input);
        System.out.println("Output: " + output);
    }
}


