using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TwoMbit {
    class Resolution {

        struct ResParams {
            public int width;
            public int height;
        };

        ResParams[] winResParams;
        ResParams[] fsResParams;
        Config cfg;
        MainWindow mainwindow;

        public Resolution(MainWindow mainwindow, Config cfg, Settings[] windowRes, Settings[] fullscreenRes) {
            this.cfg = cfg;
            this.mainwindow = mainwindow;
            winResParams = new ResParams[5];
            fsResParams = new ResParams[5];

            int i = 0;
            foreach (Settings setting in windowRes) {
                int width = 0, height = 0;
                convertResolutionToInt(setting.str_data, ref width, ref height);
                ShrinkToMaxResolution(ref width, ref height);
                winResParams[i].width = width;
                winResParams[i++].height = height;
            }
            i = 0;
            foreach (Settings setting in fullscreenRes) {
                int width = 0, height = 0;
                convertResolutionToInt(setting.str_data, ref width, ref height);
                fsResParams[i].width = width;
                fsResParams[i++].height = height;
            }
        }

        public void getResoltion(ref int width, ref int height) {
            bool fullscreen = cfg.get<bool>("fullscreen");
            int pos = !fullscreen ? cfg.get<int>("video_mode_window") : cfg.get<int>("video_mode_fullscreen");
            ResParams resparam = !fullscreen ? winResParams[pos] : fsResParams[pos];
            width = resparam.width;
            height = resparam.height;
        }

        public void ShrinkToMaxResolution(ref int width, ref int height) {

            int clientWidth = mainwindow.getDim(MainWindow.DimType.WORK_AREA_WIDTH);
            int clientHeight = mainwindow.getDim(MainWindow.DimType.WORK_AREA_HEIGHT);
            int borderWidth = mainwindow.getDim(MainWindow.DimType.WINDOW_BORDER_WIDTH);
            int borderHeight = mainwindow.getDim(MainWindow.DimType.WINDOW_CAPTION_AND_BORDER_HEIGT);

            if ((borderWidth + width) > clientWidth) {
                width = clientWidth - borderWidth;
            }
            if ((borderHeight + height) > clientHeight) {
                height = clientHeight - borderHeight;
            }
        }

        public static bool convertResolutionToInt(string resolution, ref int width, ref int height) {
            if (!isResolution(resolution)) return false;
            Regex regex = new Regex("[1-9]{1}[0-9]{2,3}");
            var matches = regex.Matches(resolution);
            int count = 0;
            foreach (Match match in matches) {
                if (count == 0) width = Convert.ToInt32(match.ToString());
                else if (count == 1) height = Convert.ToInt32(match.ToString());
                count++;
            }
            if (count != 2) return false;
            return true;
        }

        public static bool isResolution(string data) {
            Regex regex = new Regex("^[1-9]{1}[0-9]{2,3}[x][1-9]{1}[0-9]{2,3}$");
            if (regex.IsMatch(data, 0)) {
                return true;
            }
            return false;
        }
    }
}
