package com.example.petstoremobile.utils;

import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class InputValidator {

    // Checks if an EditText field is not empty
    public static boolean isNotEmpty(EditText field, String fieldName) {
        if (field.getText().toString().trim().isEmpty()) {
            field.setError(fieldName + " is required");
            field.requestFocus();
            return false;
        }
        return true;
    }

    // Checks if the value is a positive integer (>= 0)
    public static boolean isPositiveInteger(EditText field, String fieldName) {
        String value = field.getText().toString().trim();
        try {
            int num = Integer.parseInt(value);
            if (num < 0) {
                field.setError(fieldName + " must be a positive number");
                field.requestFocus();
                return false;
            }
            return true;
        } catch (NumberFormatException e) {
            field.setError(fieldName + " must be a whole number");
            field.requestFocus();
            return false;
        }
    }

    // Checks if the value is a whole number of at least 1
    public static boolean isAtLeastOne(EditText field, String fieldName) {
        String value = field.getText().toString().trim();
        try {
            int num = Integer.parseInt(value);
            if (num < 1) {
                field.setError(fieldName + " must be at least 1");
                field.requestFocus();
                return false;
            }
            return true;
        } catch (NumberFormatException e) {
            field.setError(fieldName + " must be a whole number");
            field.requestFocus();
            return false;
        }
    }

    // Checks if the value is a positive decimal number greater than 0
    public static boolean isGreaterThanZero(EditText field, String fieldName) {
        String value = field.getText().toString().trim();
        try {
            double num = Double.parseDouble(value);
            if (num <= 0) {
                field.setError(fieldName + " must be greater than 0");
                field.requestFocus();
                return false;
            }
            return true;
        } catch (NumberFormatException e) {
            field.setError(fieldName + " must be a number");
            field.requestFocus();
            return false;
        }
    }

    // Checks if the value is a positive decimal number
    public static boolean isPositiveDecimal(EditText field, String fieldName) {
        String value = field.getText().toString().trim();
        try {
            double num = Double.parseDouble(value);
            if (num < 0) {
                field.setError(fieldName + " must be a positive number");
                field.requestFocus();
                return false;
            }
            return true;
        } catch (NumberFormatException e) {
            field.setError(fieldName + " must be a number");
            field.requestFocus();
            return false;
        }
    }

    // Checks if the email address is valid
    public static boolean isValidEmail(EditText field) {
        String email = field.getText().toString().trim();
        if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            field.setError("Enter a valid email address");
            field.requestFocus();
            return false;
        }
        return true;
    }

    // Checks if the phone number is valid: XXX-XXX-XXXX, (XXX) XXX-XXXX, or XXXXXXXXXX
    public static boolean isValidPhone(EditText field) {
        String phone = field.getText().toString().trim();
        String pattern = "^(\\(\\d{3}\\) \\d{3}-\\d{4}|\\d{3}-\\d{3}-\\d{4}|\\d{10})$";
        if (phone.isEmpty() || !phone.matches(pattern)) {
            field.setError("Enter a valid phone number");
            field.requestFocus();
            return false;
        }
        return true;
    }

    // Checks if the date is in YYYY-MM-DD format
    public static boolean isValidDate(EditText field) {
        String date = field.getText().toString().trim();
        if (date.isEmpty() || !date.matches("\\d{4}-\\d{2}-\\d{2}")) {
            field.setError("Date must be in YYYY-MM-DD format");
            field.requestFocus();
            return false;
        }
        return true;
    }

    // Checks if the time format is valid (e.g. 10:00 AM)
    public static boolean isValidTime(EditText field) {
        String time = field.getText().toString().trim();
        if (time.isEmpty() || !time.matches("\\d{1,2}:\\d{2}\\s?(AM|PM|am|pm)?")) {
            field.setError("Enter a valid time (e.g. 10:00 AM)");
            field.requestFocus();
            return false;
        }
        return true;
    }

    /**
     * Checks if a selection has been made in a Spinner.
     * Assumes position 0 is a placeholder like "None" or "Select".
     */
    public static boolean isSpinnerSelected(Spinner spinner, String fieldName) {
        if (spinner.getSelectedItemPosition() <= 0) {
            View selectedView = spinner.getSelectedView();
            if (selectedView instanceof TextView) {
                TextView tv = (TextView) selectedView;
                tv.setError(fieldName + " is required");
                spinner.requestFocus();
            }
            return false;
        }
        return true;
    }
}
