/** @odoo-module **/

import { registry } from "@web/core/registry";
import { _lt } from "@web/core/l10n/translation";
import { standardFieldProps } from "@web/views/fields/standard_field_props";
import { useInputField } from "@web/views/fields/input_field_hook";
import { FileUploader } from "@web/views/fields/file_handler";
import { session } from "@web/session";
import { useService } from "@web/core/utils/hooks";
import { isBinarySize } from "@web/core/utils/binary";
import { download } from "@web/core/network/download";
import utils from 'web.utils';

import core from 'web.core';
import rpc from 'web.rpc';

var QWeb = core.qweb;

import { Component, onWillUpdateProps, useState, useRef, useEffect } from "@odoo/owl";

export class StepViewer extends Component {
   setup() {
        this.props.url = this.formatUrl();
    }

    formatUrl(){
        var url = '';
        if (this.props.value) {
            if (utils.is_bin_size(this.props.value)) {
                var url_props = {
                    base_url: session['web.base.url'],
                    model: this.props.record.resModel,
                    id: JSON.stringify(this.props.record.data['id']),
                    field: this.props.name}
                url = url_props['base_url']+'/web/content/'+url_props['model']+'/'+url_props['id']+'/'+url_props['field']+'?download=true'

            } else {
                url = "data:model/gltf-binary;base64," + this.props.value;
            }
        }
        return url
    }

}

StepViewer.template = "web_widget_model_viewer.BinaryField3d";

StepViewer.displayName = _lt("3D File");
StepViewer.supportedTypes = ["binary"];

StepViewer.props = {
    ...standardFieldProps,
    url: { type: String, optional: true },
};

StepViewer.extractProps = ({ attrs }) => {
    return {
        url: attrs.options.url,
    };
};

registry.category("fields").add("Viewer3D", StepViewer);
