using Microsoft.AspNetCore.Mvc;
using Auth.Entities;
using Auth.Services.DatabaseService;
using System.Security.Claims;

namespace Auth.Controllers {

    public class MistoxControllerBase : ControllerBase {

        public DatabaseService _databaseService;

        public MistoxControllerBase(DatabaseService databaseService) {
            _databaseService = databaseService;
        }

        public bool isLoggedIn() {
            if (User.Identity != null && User.Identity.IsAuthenticated) {
                return true;
            }
            return false;
        }

        public int getLoggedInUserID() {
            return Convert.ToInt32(User.FindFirstValue(ClaimTypes.NameIdentifier));
        }

        public async Task<Account> getLoggedInUser() {
            try {
                Account? test = await _databaseService.GetAccount(getLoggedInUserID());
                if (test != null) {
                    return test;
                }
                return new Account();
            } catch {
                return new Account();
            }
        }

        public string Substitue(string message, string subString, string Replacement) {
            for (int i = 0; i < (message.Length - subString.Length); i++) {
                if (message.Substring(i, subString.Length) == subString) {
                    string before = message.Substring(0, i);
                    string after = message.Substring(i + subString.Length);
                    return before + Replacement + after;
                }
            }
            return message;
        }

        public bool contains(string outer, string inner) {
            if (outer.Length >= inner.Length) {
                for (int i = 0; i < outer.Length - inner.Length; i++) {
                    if (outer.Substring(i, inner.Length) == inner) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}