import CRUDBase from '../CRUDBase.js'
import { session } from '../../service/Session.service.js'

class ClientResource extends CRUDBase {
    constructor() {
        super()

        this.endpoint = '/openid/clients'
        this.required_fields = ['client_name', 'grant_types', 'redirect_uri']
        this.permission_base = 'v1:openid:clients'

        this.item = 'OpenID Connect Client'
        this.plural = 'OpenID Connect Clients'

        this.listing_definition = {
            display: `
            OpenID Connect clients are applications that support authentication over the OpenID Connect protocol. This allows you to add a "Sign-In with XXX" button for ${session.get('app.name')} to the application in question. To do this, the application need only comply with the OpenID standards.
        `,
            columns: [
                {
                    name: 'Client Name',
                    field: 'client_name',
                },
                {
                    name: 'Redirect URI',
                    field: 'redirect_uri',
                },
            ],
            actions: [
                {
                    type: 'resource',
                    position: 'main',
                    action: 'insert',
                    text: 'Create New',
                    color: 'success',
                },
                {
                    type: 'resource',
                    position: 'row',
                    action: 'update',
                    icon: 'fa fa-edit',
                    color: 'primary',
                },
                {
                    type: 'resource',
                    position: 'row',
                    action: 'delete',
                    icon: 'fa fa-times',
                    color: 'danger',
                    confirm: true,
                },
            ],
        }

        this.form_definition = {
            fields: [
                {
                    name: 'Client Name',
                    field: 'client_name',
                    placeholder: 'Awesome External App',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Redirect URI',
                    field: 'redirect_uri',
                    placeholder: 'https://awesome.app/oauth2/callback',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Grant Types',
                    field: 'grant_types',
                    type: 'select.multiple',
                    options: [
                        {display: 'Refresh Token', value: 'refresh_token'},
                        {display: 'Authorization Code', value: 'authorization_code'},
                    ],
                    required: true,
                },
                {
                    name: 'Client ID',
                    field: 'client_id',
                    type: 'text',
                    readonly: true,
                    hidden: ['insert'],
                },
                {
                    name: 'Client Secret',
                    field: 'client_secret',
                    type: 'text',
                    readonly: true,
                    hidden: ['insert'],
                },
            ],
        }
    }
}

const openid_client = new ClientResource()
export { openid_client }
