# baseURI: http://tests.script.topbraid.org/actions/modifyactionwizard

@prefix dash: <http://datashapes.org/dash#> .
@prefix ex: <http://tests.script.topbraid.org/actions/modifyactionwizard#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://tests.script.topbraid.org/actions/modifyactionwizard>
  a owl:Ontology ;
  <http://topbraid.org/swa#defaultNamespace> "http://tests.script.topbraid.org/actions/modifyactionwizard#" ;
  rdfs:label "New File (ModifyActionWizard.ttl)" ;
  owl:imports <http://datashapes.org/dash> ;
.
ex:ImportActions
  a dash:ActionGroup ;
  rdfs:label "Import Actions" ;
.
ex:SimpleSpreadsheetImportAction
  a dash:ModifyAction ;
  dash:actionGroup ex:ImportActions ;
  dash:actionIconClass "fas fa-file-import" ;
  dash:js """/**
 * The begin function is the entry point of the wizard and will be called when the wizard
 * is opened, to populate the first page.
 */
function begin() {
    return DataViewers.createNextPageJSON({
		title: 'Spreadsheet Upload',
		message: 'This wizard can be used to upload a spreadsheet and then insert values from a selected column into the rdfs:comment of the focus asset.',
        params: [
            {
                varName: 'file',
                label: 'spreadsheet file',
                description: 'Upload a spreadsheet with values for the property',
                mimeTypes: '.csv,.tsv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            }
        ],
        callback: handleFirstPage,
    })
}


/**
 * This is called when the user clicks Next on the first page.
 * The state object is not used because the previous step doesn't create one.
 */
function handleFirstPage(state) {
	// Here the file variable is the value from the parameter of the first page
    let s = IO.uploadedFile(file).asSpreadsheet();
    return DataViewers.createNextPageJSON({
		title: 'Select Column to Import',
		message: 'Select the column that contains the values that you want to import',
        last: true,  // No more Next button, allow Ok and Preview
        params: [
            {
                varName: 'column',
                label: 'select column',
                datatype: xsd.string,
                description: 'Select the column that contains the values',
				enumValues: s.columnNames().map(name => graph.literal(name)),
            }
        ],
        state: { // Pass a state object into the next callback
            file: file
        },
        callback: handleOk,
    })
}


/**
 * This is called when the user clicks OK on the second page.
 * As this is a modify action, this is the only place where modifications to the graph
 * can be made.  The other functions execute in read-only mode.
 */
function handleOk(state) {
	// Here the column variable is the value from the parameter of the second page
	let columnName = column;

	// The state object holds the reference to the file that was uploaded on page one
    let s = IO.uploadedFile(state.file).asSpreadsheet();

	// Iterate over all rows to copy their values into rdfs:comment of the focusNode
	s.rows().forEach(row => {
		focusNode.add(rdfs.comment, row[columnName]);
	})
}
""" ;
  dash:wizard true ;
  rdfs:comment """A simple demo illustrating how to implement multi-page wizards in ADS Modify Actions.

The action can be called from any resource (i.e. it is attached to rdfs:Resource) and opens a wizard.
On the first page, the user must upload a spreadsheet.
The callback that produces the second page then looks at the columns of the spreadsheet
and produces a drop down of these columns.
On the final step, the values of the selected column are added as rdfs:comments to the focus node.""" ;
  rdfs:label "Simple Spreadsheet Import..." ;
.
rdfs:Resource
  dash:resourceAction ex:SimpleSpreadsheetImportAction ;
.
