package utils

const (
	minUsername        = "2"
	maxUsername        = "20"
	MinUsername        = 2
	MaxUsername        = 20
	minPassword        = "6"
	MinPassword        = 6
	maxPassword        = "64"
	MaxPassword        = 64
	minScannerName     = "3"
	maxScannerName     = "16"
	MinRoleDisplayName = 3
	MaxRoleDisplayName = 30
	MaxRoleDescription = 80

	LenHeaderXAuthorization = 36
	lenHeaderXAuthorization = "36"
	LenUserId               = 36
	LenHeaderXApiKey        = 36

	HeaderXAuthorization = "X-Authorization"
	HeaderXApiKey        = "X-Api-Key"

	MaxAvatarSize = 5 * 1024 * 1024 // 5 MB

	GroupTaskLockedTime  = 3
	SessionExpiresAtTime = 7 * 24 * 60 * 60 // 1 week

	LogTypeInfo  = 0
	LogTypeError = 1

	ConnectionStateOffline = 0
	ConnectionStateOnline  = 1
)

var (
	AcceptedAvatarFileTypes = []string{"image/png", "image/jpeg", "image/jpg", "image/gif"}
)

// commands sent to web clients
const (
	SentCmdInitUserSocketConnection               = 1
	SentCmdUpdateConnectedUsers                   = 2
	SentCmdNewGroupTaskStarted                    = 3
	SentCmdNewGroupTaskStep                       = 4
	SentCmdUpdateGroupTaskStep                    = 5
	SentCmdUpdateGroupTask                        = 6
	SentCmdReloadingGroupTasks                    = 7
	SentCmdGroupTasksReloaded                     = 8
	SentCmdUpdateUserSessions                     = 9
	SentCmdUpdateAllUsersUserAvatar               = 10
	SentCmdNewScanner                             = 11
	SentCmdDeleteScanner                          = 12
	SentCmdUpdateScannerUsedBy                    = 13
	SentCmdScanResult                             = 14
	SentCmdUpdateScannerLastUsed                  = 15
	SentCmdTaskLocked                             = 16
	SentCmdTaskUnlocked                           = 17
	SentCmdUserProfileUpdated                     = 18
	SentCmdAdminAreaNewRoleCreated                = 19
	SentCmdAdminAreaRoleUpdated                   = 20
	SentCmdAdminAreaUpdateRoleSortingOrder        = 21
	SentCmdAdminAreaRoleDeleted                   = 22
	SentCmdAllUsersUserRoleUpdated                = 23
	SentCmdRolePermissionsUpdated                 = 24
	SentCmdErrorNoPermissions                     = 25
	SentCmdAllUsersNewUserCreated                 = 26
	SentCmdAllUsersUserDeleted                    = 27
	SentCmdAllUsersUserDeactivation               = 28
	SentCmdGroupTasksCategoryGroupChanges         = 29
	SentCmdNewUserApiKeyCreated                   = 30
	SentCmdDeletedUserApiKey                      = 31
	SentCmdNewApiKeyUsageCount                    = 32
	SentCmdInstallingPythonPackages               = 33
	SentCmdInstallingPythonPackagesFailed         = 34
	SentCmdInstallingPythonPackagesFinished       = 35
	SentCmdInstallingGlobalPythonPackages         = 36
	SentCmdInstallingGlobalPythonPackagesFailed   = 37
	SentCmdInstallingGlobalPythonPackagesFinished = 38
)

// commands received from web clients
const (
	ReceivedCmdStartGroupTasks                           = 1
	ReceivedCmdTaskFailedTryAgainRunTaskStep             = 2
	ReceivedCmdTaskContinueTaskStep                      = 3
	ReceivedCmdReloadGroupTasks                          = 4
	ReceivedCmdTaskLocking                               = 5
	ReceivedCmdUpdateUserProfile                         = 6
	ReceivedCmdAdminAreaCreateNewRole                    = 7
	ReceivedCmdAdminAreaUpdateRole                       = 8
	ReceivedCmdAdminAreaUpdateRoleSortingOrder           = 9
	ReceivedCmdAdminAreaDeleteRole                       = 10
	ReceivedCmdAllUsersUpdateUserRole                    = 11
	ReceivedCmdAllUsersCreateNewUser                     = 12
	ReceivedCmdAllUsersDeleteUser                        = 13
	ReceivedCmdAllUsersUserDeactivation                  = 14
	ReceivedCmdScannersUseScanners                       = 15
	ReceivedCmdScannersDisconnectScanner                 = 16
	ReceivedCmdGroupTasksCheckingForCategoryGroupChanges = 17
	ReceivedCmdHandleUserActionTaskStep                  = 18
	ReceivedCmdCreateNewUserApiKey                       = 19
	ReceivedCmdDeleteUserApiKey                          = 20
	ReceivedCmdGroupTasksInstallPythonPackages           = 21
	ReceivedCmdGroupTasksInstallGlobalPythonPackages     = 22
)

const (
	GroupTasksStatusFinished      uint8 = 1
	GroupTasksStatusRunning       uint8 = 2
	GroupTasksStatusCanceled      uint8 = 3
	GroupTasksStatusFailed        uint8 = 4
	GroupTasksStatusInputRequired uint8 = 5
	GroupTasksStatusPaused        uint8 = 6
	GroupTasksStatusUndoEnded     uint8 = 7
)

var (
	generalRules = map[string]string{
		"Username":    "required,min=" + minUsername + ",max=" + maxUsername,
		"Password":    "required", // length is checked later because sent in base64
		"SessionId":   "required,len=" + lenHeaderXAuthorization,
		"ScannerName": "required,min=" + minScannerName + ",max=" + maxScannerName,
	}

	groupTaskRules = map[string]string{
		"Category":     "required",
		"GroupId":      "required",
		"Description":  "required",
		"GlobalInputs": "required",
	}

	equipmentRules = map[string]string{
		"StockItemId": "required",
	}
)

const (
	_equipmentDocumentation                             = "equipment_documentation."
	PermissionEquipmentDocumentationView                = _equipmentDocumentation + "view"
	_groupTasks                                         = "group_tasks."
	PermissionGroupTasksOverviewXYNewTask               = _groupTasks + "overview.XY.new_task"
	PermissionGroupTasksOverviewXYReloadGroupConfig     = _groupTasks + "overview.XY.reload_group_config"
	PermissionGroupTasksOverviewXYInstallPythonPackages = _groupTasks + "overview.XY.install_python_packages"
	PermissionGroupTasksOverviewXYView                  = _groupTasks + "overview.XY.view"
	PermissionGroupTasksHistory                         = _groupTasks + "history"
	PermissionGroupTasksInstallGlobalPythonPackages     = _groupTasks + "install_global_python_packages"
	PermissionGroupTasksCheckingForCategoryGroupChanges = _groupTasks + "checking_for_category_group_changes"
	PermissionAllUsersCreateNewUser                     = "all_users.create_new_user"
	PermissionAllUsersActionChangeRole                  = "all_users.action.change_role"
	PermissionAllUsersActionDeleteUser                  = "all_users.action.delete_user"
	PermissionAllUsersActionUserDeactivation            = "all_users.action.user_deactivation"
	PermissionScannerUseScanners                        = "scanner.use_scanners"

	_adminArea                        = "admin_area."
	_adminAreaRoles                   = _adminArea + "roles."
	PermissionAdminAreaCreateNewRole  = _adminAreaRoles + "create_new_role"
	PermissionAdminAreaUpdateRole     = _adminAreaRoles + "update_role"
	PermissionAdminAreaDeleteRole     = _adminAreaRoles + "delete_role"
	PermissionAdminAreaMoveRoleUpDown = _adminAreaRoles + "move_role_up_down"
	PermissionAdminAreaLogs           = _adminArea + "logs"

	PermissionUserProfileApiKeys = "user_profile.api_keys"
)

var SystemPermissions = []string{
	PermissionEquipmentDocumentationView,
	PermissionGroupTasksHistory,
	PermissionGroupTasksInstallGlobalPythonPackages,
	PermissionGroupTasksCheckingForCategoryGroupChanges,
	PermissionAllUsersActionChangeRole,
	PermissionAllUsersActionDeleteUser,
	PermissionAllUsersActionUserDeactivation,
	PermissionAllUsersCreateNewUser,
	PermissionScannerUseScanners,
	PermissionAdminAreaCreateNewRole,
	PermissionAdminAreaUpdateRole,
	PermissionAdminAreaDeleteRole,
	PermissionAdminAreaMoveRoleUpDown,
	PermissionAdminAreaLogs,
	PermissionUserProfileApiKeys,
}

var DynamicGroupTasksPermissions = []string{
	PermissionGroupTasksOverviewXYNewTask,
	PermissionGroupTasksOverviewXYReloadGroupConfig,
	PermissionGroupTasksOverviewXYInstallPythonPackages,
	PermissionGroupTasksOverviewXYView,
}
