'''
Welcome to Advanced Debug.
Licensed with MIT

Change Logs:
	- Hot-fix of 0.1.7
    - Added file logging feature
    - Added new style "Minimalist"
'''

__version__ = '0.1.8'

import os
from datetime import datetime
from enum import Enum

import random

listPhrases = ['Writting some lines.', "Don't forgot to define!", 'Waiting for your command.']

randomPhrase = random.choice(listPhrases)

class AdvDBG:
	
	def __init__(self, title='DEBUG', activated=False, notify=False, logging=True, legacy=False):
		if isinstance(title, str) and isinstance(activated, bool) and isinstance(notify, bool):
			self.title = title
			self.activated = False
			self._defed = False
			self.notify = notify
			self.logging = logging
			self.legacy = legacy
		else:
			return False, 'Some parameters is not matches with required '
		
	@classmethod
	def define(cls, title='DEBUG', activated=True, notify=False, logging=True, legacy=False):
		'''Defines your debug category.
		:param title: Title of your category
		:param activated: Toggle availability of category
		:param notify: Toggles notification if category is not activated
		:param logging: Toggles is it logging
		:param legacy: Use old style?'''
		inst = cls(title, activated, notify, logging, legacy)
		inst.title = title
		inst.activated = True
		inst.notify = notify
		inst.logging = logging
		inst.legacy = legacy
		return inst
	
	def _write_to_log(self, text, log_type='INFO'):
		'''For-Module only method.'''
		if not self.logging:
			return
		
		try:
			logs_dir = os.path.join(os.path.dirname(__file__), 'logs')
			os.makedirs(logs_dir, exist_ok=True)
			
			current_date = datetime.now().strftime("%d.%m.%Y")
			filename = f"log_{current_date}.txt"
			filepath = os.path.join(logs_dir, filename)
			
			current_time = datetime.now().strftime("%H:%M:%S")
			log_entry = f"{self.title} {current_time} | {text}\n"
			
			file_exists = os.path.exists(filepath)
			
			with open(filepath, 'a', encoding='utf-8') as f:
				if not file_exists:
					f.write(f"/// Advanced Debugger\n")
					f.write(f"Category: {self.title}\n")
					f.write(randomPhrase)
					f.write("\n")
				
				f.write(log_entry)
				
		except Exception as e:
			print(f"\033[33mWARNING\033[0m | Failed to write to log file: {e}")
		
	def info(self, text):
			"""Print debug information
			Type: INFO
		
			:param text: Text to show"""
			if not isinstance(text, str):
				try:
						text = str(text)
				except Exception as e:
						text = f"Cannot convert to string format: {e}"
		
			if self.activated:
				if self.legacy:
					print(f'[\033[90mINFO \033[95m{self.title} at {datetime.now().strftime("%D, %H:%M:%S")}\033[0m] {text} \033[0m')
				elif not self.legacy:
					print(f' Info - {self.title} ({datetime.now().strftime("%D, %H:%M:%S")}) | {text}')
				if self.logging:
					self._write_to_log(text, 'INFO')
			elif self.notify:
				print(f'Notification from {self.title}: Tryed to output when disactivated.\n\033[93mTip: \033[0mIf you are did not want to saw these notifications, turn off NOTIFY property with using notify=False\033[0m')
			else:
				return
				
	def warn(self, text):
			if not isinstance(text, str):
					try:
						text = str(text)
					except Exception as e:
						text = f"Cannot convert to string format: {e}"
			
			if self.activated:
				if self.legacy:
					print(f'[\033[90mWARN \033[95m{self.title} at {datetime.now().strftime("%D, %H:%M:%S")}\033[0m] {text} \033[0m')
				elif not self.legacy:
					print(f' Warn - {self.title} ({datetime.now().strftime("%D, %H:%M:%S")}) | {text}')
				if self.logging:
					self._write_to_log(text, 'WARN')
			elif self.notify:
				print(f'Notification from {self.title}: Tryed to output when disactivated.\n\033[93mTip: \033[0mIf you are did not want to saw these notifications, turn off NOTIFY property with using notify=False')
			else:
				return
				
	def error(self, text):
			if not isinstance(text, str):
					try:
						text = str(text)
					except Exception as e:
						text = f"Cannot convert to string format: {e}"
						
			if self.activated:
				if self.legacy:
					print(f'[\033[33mERROR \033[95m{self.title} at {datetime.now().strftime("%D, %H:%M:%S")}\033[0m] {text} \033[0m')
				elif not self.legacy:
					print(f' Error - {self.title} ({datetime.now().strftime("%D, %H:%M:%S")}) | {text}')
				if self.logging:
					self._write_to_log(text, 'ERROR')
			elif self.notify:
				print(f'Notification from {self.title}: Tryed to output when disactivated.\n\033[93mTip: \033[0mIf you are did not want to saw these notifications, turn off NOTIFY property with using notify=False')
			else:
				return
		
	def notification(self, text):
			if not isinstance(text, str):
					try:
						text = str(text)
					except Exception as e:
						text = f"Cannot convert to string format: {e}"
						
			if self.activated:
				print(f'[\033[94mNOTIFICATION \033[0m{self.title} at {datetime.now().strftime("%D, %H:%M:%S")}\033[0m] \033[94m{text}\033[0m')
				if self.logging:
					self._write_to_log(text, 'NOTIFICATION')
			elif self.notify:
				print(f'Notification from {self.title}: Tryed to output when disactivated.\n\033[93mTip: \033[0mIf you are did not want to saw these notifications, turn off NOTIFY property with using notify=False')
			else:
				return
		
	def cfg(self, activated=None, title='DEBUG', notify=True, logging=True):
		'''Configure existing category'''
		if activated is not None:
			self.activated = activated
			if self.activated is False:
				self.notify = notify
		elif title is not None:
			self.title = title
		elif logging is not None:
			self.logging = logging
		return self
			
	def __call__(self, text):
		if not isinstance(text, str):
					try:
						text = str(text)
					except Exception as e:
						text = f"Cannot convert to string format: {e}"
		self.info(text)

DebuGGer = AdvDBG