import "#elements/Tabs";
import "#admin/rbac/ak-rbac-object-permission-page";
import "#admin/events/ObjectChangelog";
import "#elements/forms/ModalForm";
import "#admin/policies/BoundPoliciesList";
import "#admin/sources/telegram/TelegramSourceForm";

import { DEFAULT_CONFIG } from "#common/api/config";

import { AKElement } from "#elements/Base";
import { sourceBindingTypeNotices } from "#elements/sources/utils";

import { ModelEnum, SourcesApi, TelegramSource } from "@goauthentik/api";

import { msg } from "@lit/localize";
import { CSSResult, html, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators.js";

import PFButton from "@patternfly/patternfly/components/Button/button.css";
import PFCard from "@patternfly/patternfly/components/Card/card.css";
import PFContent from "@patternfly/patternfly/components/Content/content.css";
import PFDescriptionList from "@patternfly/patternfly/components/DescriptionList/description-list.css";
import PFPage from "@patternfly/patternfly/components/Page/page.css";
import PFGrid from "@patternfly/patternfly/layouts/Grid/grid.css";

@customElement("ak-source-telegram-view")
export class TelegramSourceViewPage extends AKElement {
    @property({ type: String })
    set sourceSlug(value: string) {
        new SourcesApi(DEFAULT_CONFIG)
            .sourcesTelegramRetrieve({
                slug: value,
            })
            .then((source) => {
                this.source = source;
            });
    }

    @property({ attribute: false })
    source?: TelegramSource;

    public static styles: CSSResult[] = [
        // ---
        PFPage,
        PFButton,
        PFGrid,
        PFContent,
        PFCard,
        PFDescriptionList,
    ];

    render(): TemplateResult {
        if (!this.source) {
            return html``;
        }
        return html` <ak-tabs>
            <section
                slot="page-overview"
                data-tab-title="${msg("Overview")}"
                class="pf-c-page__main-section pf-m-no-padding-mobile"
            >
                <div class="pf-l-grid pf-m-gutter">
                    <div class="pf-c-card pf-l-grid__item pf-m-12-col">
                        <div class="pf-c-card__body">
                            <dl class="pf-c-description-list pf-m-2-col-on-lg">
                                <div class="pf-c-description-list__group">
                                    <dt class="pf-c-description-list__term">
                                        <span class="pf-c-description-list__text"
                                            >${msg("Name")}</span
                                        >
                                    </dt>
                                    <dd class="pf-c-description-list__description">
                                        <div class="pf-c-description-list__text">
                                            ${this.source.name}
                                        </div>
                                    </dd>
                                </div>
                                <div class="pf-c-description-list__group">
                                    <dt class="pf-c-description-list__term">
                                        <span class="pf-c-description-list__text"
                                            >${msg("Telegram bot")}</span
                                        >
                                    </dt>
                                    <dd class="pf-c-description-list__description">
                                        <div class="pf-c-description-list__text">
                                            ${this.source.botUsername}
                                        </div>
                                    </dd>
                                </div>
                            </dl>
                        </div>
                        <div class="pf-c-card__footer">
                            <ak-forms-modal>
                                <span slot="submit">${msg("Save Changes")}</span>
                                <span slot="header">${msg("Update Telegram Source")}</span>
                                <ak-source-telegram-form
                                    slot="form"
                                    .instancePk=${this.source.slug}
                                >
                                </ak-source-telegram-form>
                                <button slot="trigger" class="pf-c-button pf-m-primary">
                                    ${msg("Edit")}
                                </button>
                            </ak-forms-modal>
                        </div>
                    </div>
                </div>
            </section>
            <section
                slot="page-changelog"
                data-tab-title="${msg("Changelog")}"
                class="pf-c-page__main-section pf-m-no-padding-mobile"
            >
                <div class="pf-l-grid pf-m-gutter">
                    <div class="pf-c-card pf-l-grid__item pf-m-12-col">
                        <ak-object-changelog
                            targetModelPk=${this.source.pk || ""}
                            targetModelName=${ModelEnum.AuthentikSourcesTelegramTelegramsource}
                        >
                        </ak-object-changelog>
                    </div>
                </div>
            </section>
            <div
                slot="page-policy-binding"
                data-tab-title="${msg("Policy Bindings")}"
                class="pf-c-page__main-section pf-m-no-padding-mobile"
            >
                <div class="pf-l-grid pf-m-gutter">
                    <div class="pf-c-card pf-l-grid__item pf-m-12-col">
                        <div class="pf-c-card__title">
                            ${msg(
                                `These bindings control which users can access this source.
            You can only use policies here as access is checked before the user is authenticated.`,
                            )}
                        </div>
                        <ak-bound-policies-list
                            .target=${this.source.pk}
                            .typeNotices=${sourceBindingTypeNotices()}
                            .policyEngineMode=${this.source.policyEngineMode}
                        >
                        </ak-bound-policies-list>
                    </div>
                </div>
            </div>
            <ak-rbac-object-permission-page
                slot="page-permissions"
                aria-label="${msg("Permissions")}"
                model=${ModelEnum.AuthentikSourcesTelegramTelegramsource}
                objectPk=${this.source.pk}
            ></ak-rbac-object-permission-page>
        </ak-tabs>`;
    }
}

declare global {
    interface HTMLElementTagNameMap {
        "ak-source-telegram-view": TelegramSourceViewPage;
    }
}
