export const mixinDefault = {
    data() {
        return {
            isLoading: false,
            isFailed: false,
            httpMessage: '',
            httpType: ''
        }
    },
    methods: {
        startLoading() {
            this.isLoading = true
            this.isFailed = false
            this.httpMessage = ''
            this.httpType = ''
        },
        completeLoading(response) {
            this.isLoading = false
            if (response) {
                this.httpMessage = (response.body) ? this.$t('events.'+response.body.message) : response
                switch (response.status) {
                    case 0:
                    case 404:
                        this.httpType = 'is-warning'
                        this.httpMessage = this.$t('message.server_404')
                        break
                    case 401:
                        this.httpType = 'is-warning'
                        this.httpMessage = this.$t('message.server_401')
                        break
                    case 403:
                        this.httpType = 'is-warning'
                        this.httpMessage = this.$t('message.server_403')
                        break
                    case 400:
                        this.httpType = 'is-warning'
                        this.httpMessage = this.$t('message.server_400')
                        response.body.fields.forEach(e => {this.httpMessage += "<p>- " + e.message + " </p>"});
                        break
                    case 422:
                    case 500:
                        this.httpType = 'is-danger'
                        break
                    case 200:
                    case 202:
                        this.httpType = 'is-info'
                        break
                    case 201:
                        this.httpType = 'is-success'
                        break
                }
            }
        },
        completeLoadingSuccess(res, options) {
            this.completeLoading(res)
            if (res && options.notification) {
                this.notification()
            }
        },
        completeLoadingError(res, options) {
            this.completeLoading(res)
            this.isFailed = true
            if (options.notification) {
                this.notification()
            }
            if (res.body && res.body.fields) {
                // TODO: Pensar em uma forma de remover a validação após interação (Usar o remove no on focus é uma opção)
                res.body.fields.forEach(e => { this.errors.add(e.field, e.message) })
            }
        },
        httpGet(url, options = { notification: true }) {
            this.startLoading()
            return this.$http.get(url, this.httpHeaders).then(
                (res) => {
                    this.completeLoading()
                    return Promise.resolve(res)
                },
                (res) => {
                    this.completeLoadingError(res, options)
                    return Promise.reject(res)
                }
            );
        },
        httpPost(url, body, options = { notification: true }) {
            this.startLoading()
            return this.$http.post(url, body || {}, this.httpHeaders).then(
                (res) => {
                    this.completeLoadingSuccess(res, options)
                    return Promise.resolve(res)
                }, (res) => {
                    this.completeLoadingError(res, options)
                    return Promise.reject(res)
                })
        },
        httpPut(url, body, options = { notification: true }) {
            this.startLoading()
            return this.$http.put(url, body || {}, this.httpHeaders).then(
                (res) => {
                    this.completeLoadingSuccess(res, options)
                    return Promise.resolve(res)
                }, (res) => {
                    this.completeLoadingError(res, options)
                    return Promise.reject(res)
                })
        },
        httpDelete(url, options = { notification: true }) {
            this.startLoading()
            return this.$http.delete(url, this.httpHeaders).then(
                (res) => {
                    this.completeLoadingSuccess(res, options)
                    return Promise.resolve(res)
                }, (res) => {
                    this.completeLoadingError(res, options)
                    return Promise.reject(res)
                })
        },
        notification() {
            let duration = 7 * 1000
            this.$buefy.notification.open({
                duration,
                message: this.httpMessage,
                type: this.httpType,
                position: 'is-top'
            })
        },
    },
    computed: {
        httpHeaders() {
            return {
                headers: {
                    "Authorization": "Bearer " + ((this.$keycloak) ? this.$keycloak.token : '')
                },
            }
        },
        roles() {
            return (this.$keycloak) ? this.$keycloak.resourceAccess["leanisp-api"].roles : []
        },
        user () {
            return (this.$keycloak) ? (this.$keycloak.tokenParsed || {}) : {}
        }
    }
}
