import {Component, inject, input} from '@angular/core';
import {TuiButton, TuiGroup, TuiIcon} from '@taiga-ui/core';
import {permissionGranted, permissions} from '@chatenium/chatenium-sdk/core/permissions';
import {NetworkStorage, ServiceManager} from '../../../service-manager';
import {Session} from '@chatenium/chatenium-sdk/domain/sessionManager.schema';
import {SessionManager} from '@chatenium/chatenium-sdk/services/sessionManager';
import {TranslatePipe} from '@ngx-translate/core';
import {Overview} from './overview/overview';

@Component({
  selector: 'network-settings',
  imports: [
    TuiGroup,
    TuiIcon,
    TranslatePipe,
    TuiButton,
    Overview
  ],
  templateUrl: './settings.html',
  styleUrl: './settings.scss',
})
export class Settings {
  serviceManager = inject(ServiceManager)
  networkStore = input.required<NetworkStorage>()

  selectedOption = "overview"

  optionRequiredPermissionsGranted(permissions: number, required: number[]) {
    if (!this.serviceManager.currentSession) {
      return false
    }

    let granted = false;

    if (this.networkStore().networkData().createdBy == this.serviceManager.currentSession()!.userData.userid) {
      return true
    }

    required.forEach((permission) => {
      if (permissionGranted(permissions, permission)) {
        granted = true
      }
    })

    return granted
  }

  networkSettingsOptions: {
    name: string,
    icon: string,
    options: {
      name: string, // ? channel.settings.[name]
      icon: string // ? Class name
      requiredAtLeastOneOf?: number[],
      implemented: boolean
    }[]
  }[] = [
    {
      name: "networkName", // ? UI Will show network name
      icon: "network",
      options: [
        {
          icon: "eye",
          name: "overview",
          implemented: true,
          requiredAtLeastOneOf: [permissions.changeNetworkNamePictureAndVisibility]
        },
        {
          name: "rank",
          icon: "book",
          requiredAtLeastOneOf: [permissions.createAndEditRanks, permissions.deleteRanks],
          implemented: false,
        },
        {
          name: "emoji",
          icon: "smile",
          implemented: false,
          requiredAtLeastOneOf: [permissions.createEmojis]
        },
        {
          name: "embed",
          icon: "code",
          implemented: false,
          requiredAtLeastOneOf: [permissions.manageEmbed]
        },
        {
          name: "invite",
          icon: "ticket",
          implemented: false,
          requiredAtLeastOneOf: [permissions.createInvites, permissions.deleteInvites]
        }
      ]
    },
    {
      name: "apps",
      icon: "layout-panel-left",
      options: [
        {
          name: "webhook",
          icon: "globe",
          implemented: false,
          requiredAtLeastOneOf: [permissions.deleteWebhooks, permissions.createWebhooks]
        },
        {
          name: "bots",
          icon: "bot",
          implemented: false,
        }
      ]
    },
    {
      name: "moderation",
      icon: "shield-half",
      options: [
        {
          name: "activityHistory",
          icon: "scroll-text",
          implemented: false,
        },
        {
          name: "bans",
          icon: "gavel",
          implemented: false,
          requiredAtLeastOneOf: [permissions.banMembers]
        }
      ]
    },
    {
      name: "community",
      icon: "users",
      options: [
        {
          name: "communityChannels",
          icon: "hash",
          implemented: false,
        },
        {
          name: "networkIntroducer",
          icon: "network",
          implemented: false,
        },
        {
          name: "members",
          icon: "users",
          implemented: false,
        }
      ]
    }
  ]
}
