/** @odoo-module **/

import { browser } from "@web/core/browser/browser";
import { Dialog } from "@web/core/dialog/dialog";
import { _lt } from "@web/core/l10n/translation";
import { useChildRef, useOwnedDialogs, useService } from "@web/core/utils/hooks";
import { sprintf } from "@web/core/utils/strings";
import { isMobileOS } from "@web/core/browser/feature_detection";
import * as BarcodeScanner from "@web/webclient/barcode/barcode_scanner";

const {xml, Component} = owl;
import { standardFieldProps } from "@web/views/fields/standard_field_props";
// Import the registry
import {registry} from "@web/core/registry";


export class CodeField extends Component {
    setup() {
        super.setup();
    }
     async onBarcodeBtnClick() {
        // console.log(BarcodeScanner)
        const barcode = await BarcodeScanner.scanBarcode();
        // console.log(typeof barcode)
        // alert(barcode)

        if (barcode) {
            // console.log("存在")
            // alert('存在')

             await this.onBarcodeScanned(barcode);
            if ("vibrate" in browser.navigator) {
                browser.navigator.vibrate(100);
            }
        } else {
            // console.log("不存在")
            // alert('不存在')
            this.notification.add(this.env._t("Please, scan again !"), {
                type: "warning",
            });
        }
    }
    async search(barcode) {
        const results = await this.orm.call("sf.tray", "name_search", [code], {
            name: barcode,
            args: this.getDomain(),
            operator: "ilike",
            limit: 2, // If one result we set directly and if more than one we use normal flow so no need to search more
            context: this.context,
        });
        return results.map((result) => {
            const [id, displayName] = result;
            return {
                id,
                name: displayName,
            };
        });
    }
    async onBarcodeScanned(barcode) {
        const results = await this.search(barcode);
        const records = results.filter((r) => !!r.id);
        if (records.length === 1) {
            this.update([{ id: records[0].id, name: records[0].name }]);
        } else {
            const searchInput = this.autocompleteContainerRef.el.querySelector("input");
            searchInput.value = barcode;
            searchInput.dispatchEvent(new Event("input"));
            if (this.env.isSmall) {
                searchInput.click();
            }
        }
    }
}

CodeField.template = xml`
                <button
                    t-on-click="onBarcodeBtnClick"
                    type="button"
                    class="btn ms-3 o_barcode"
                    tabindex="-1"
                    draggable="false"
                    aria-label="Scan barcode"
                    title="Scan barcode"
                    data-tooltip="Scan barcode"
                />
`;
// CodeField.template = 'sf_machine_connect.CodeField';
CodeField.props = standardFieldProps;

// Add the field to the correct category
registry.category("fields").add("code", CodeField);