package ua.nure.jfn.task3;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public final class Utils {

    private static final String ENCODING = "Cp1251";

    private Utils() {

    }

    public static int parseInt(String s, int base) {
        int num = 0;
        int pow = 1;

        char[] chArray = s.toCharArray();
        for (int i = chArray.length - 1; i >= 0; i--) {
            char ch = chArray[i];
            if (ch >= '0' && ch <= '9') {
                num += (ch - '0') * pow;
                pow *= base;
            } else if (ch >= 'A' && ch <= 'Z') {
                num += (ch - 'A' + 10) * pow;
                pow *= base;
            }
        }

        return num;
    }

    public static int parseInt(String s) {
        return parseInt(s, 10);
    }

    public static String changeCase(String s) {
        String arr1 = "abcdefghijklmnopqrstuvwxyzабвгґдеєжзиіїйклмнопрстуфхцчшщьюяABCDEFGHIJKLMNOPQRSTUVWXYZАБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯ";
        String arr2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZАБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯabcdefghijklmnopqrstuvwxyzабвгґдеєжзиіїйклмнопрстуфхцчшщьюя";

        String result = "";

        for (char c : s.toCharArray()) {
            int i = arr1.indexOf(c);
            if (i != -1)
                c = arr2.charAt(i);

            result += c;
        }

        return result;
    }

    public static boolean contains(String o, String[] arr) {
        for (String e : arr)
            if (e != null && e.equals(o))
                return true;

        return false;
    }

    public static String copyOfRange(String s, int start, int end) {
        char[] arr = s.toCharArray();

        char[] result = new char[end - start];
        for (int i = 0; i < end - start; i++)
            result[i] = arr[start + i];

        return new String(result);
    }

    public static int max(int a, int b) {
        return a > b ? a : b;
    }

    public static String getContent(String path) {
        String res = null;
        try {
            byte[] bytes = Files.readAllBytes(Paths.get(path));
            res = new String(bytes, ENCODING);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(getContent("part1.txt"));
    }
}
