/*******************************************************************************
 * IBM Confidential
 * 
 * OCO Source Materials
 * 
 * A and PM: PD
 * 
 * (c) Copyright IBM Corp. 2015
 * 
 * The source code for this program is not published or otherwise divested of
 * its trade secrets, irrespective of what has been deposited with the U.S.
 * Copyright Office.
 ******************************************************************************/

require([
	"dojo/_base/declare", 
	"pd/ui/_base",
	"pd/controller/mdsrvRequest",
	"pd/data/PdColumnAnalyser",
	"pd/widgets/MDAnalyseDialog",
	"dojo/domReady!"
],function(declare, _base, mdsrvRequest, PdColumnAnalyser, MDAnalyseDialog){
	declare("pd/ui/metadataEditor", [_base], {
		mode: pd.statics.MODE_EDIT,
		mdAnalyseDlg: null,
		
		renderMainContent: function(pdSpecValue) {
			if (this.mode == pd.statics.MODE_REFRESH) {
				this.showAnalyseDialog(pdSpecValue);
			} else {
				this._renderMainContent(pdSpecValue);
				this._setRefreshMode();
			}
		},
		
		_setRefreshMode: function() {
			this.mode = pd.statics.MODE_REFRESH;
			pd_uploader.uploadUrl = g_pd_gateway + "/metadataUIService?pid=pdm_process&c=processLOBData";
			pd_uploader.uploadUrl += "&dataSetStoreID="+encodeURIComponent(this.dataSetStoreID);
			pd_uploader.uploadUrl += "&repositoryName="+encodeURIComponent(this.repositoryName);
			pd_uploader.uploadUrl += "&repositoryConnection="+encodeURIComponent(this.repositoryConnection);
			pd_uploader.uploadUrl += "&repositorySignon="+encodeURIComponent(this.repositorySignon);
		},
		
		getTargetUrl: function() {
			return g_pd_gateway + "/metadataUIService?pid=pdm_process&c=readLOBDataSpec"
				+ "&dataSetStoreID="+encodeURIComponent(this.dataSetStoreID)
				+ "&repositoryName="+encodeURIComponent(this.repositoryName)
				+ "&repositoryConnection="+encodeURIComponent(this.repositoryConnection)
				+ "&repositorySignon="+encodeURIComponent(this.repositorySignon);
				
		},
		
		showAnalyseDialog: function(pdSpecValue) {
			var analyser = new PdColumnAnalyser({xmlInput: pdSpecValue});
			var prunedPdSpec = analyser.getSpecFromXml();
			this.mdAnalyseDlg = new MDAnalyseDialog({
				okHandler: dojo.hitch(this, this._applyChange, pdSpecValue, prunedPdSpec),
				cancelHandler: dojo.hitch(this, this._cancelChange),
				delta: analyser.getDeltaFromXml()
			});
			this.mdAnalyseDlg.show();
		},
		
		_closeAnalyseDialog: function(pdSpec) {
			if (this.mdAnalyseDlg){
				this.mdAnalyseDlg.hide();
			}
			this._renderMainContent(pdSpec);
		},
		
		_applyChange: function(pdSpec, pdPrunedSpec) {
			mdsrvRequest.saveDataSet(this.dataSetStoreID, this.repositoryName, this.repositoryConnection, this.repositorySignon, pdPrunedSpec, 
				dojo.hitch(this, this._closeAnalyseDialog, pdSpec));
		},
		
		_cancelChange: function() {
			mdsrvRequest.cancelDataSet(this.dataSetStoreID, this.repositoryName, this.repositoryConnection, this.repositorySignon);
		},
		
		startup: function() {
			this.inherited(arguments);
			this.setStatus(pd.statics.STATUS_PROCESS);
			pd_uploader.submit();
		},
		
		getDataSetName: function() {
			return this.dataSetName;
		}
	});
});