// COPYRIGHT © 2018 Esri
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// This material is licensed for use under the Esri Master License
// Agreement (MLA), and is bound by the terms of that agreement.
// You may redistribute and use this code without modification,
// provided you adhere to the terms of the MLA and include this
// copyright notice.
//
// See use restrictions at http://www.esri.com/legal/pdfs/mla_e204_e300/english
//
// For additional information, contact:
// Environmental Systems Research Institute, Inc.
// Attn: Contracts and Legal Services Department
// 380 New York Street
// Redlands, California, USA 92373
// USA
//
// email: contracts@esri.com
//
// See http://js.arcgis.com/4.10/esri/copyright.txt for details.

define(["require","exports","../../../../core/tsSupport/declareExtendsHelper","../../../../core/tsSupport/decorateHelper","../../../../geometry","../../../../Graphic","../../../../core/Accessor","../../../../core/Evented","../../../../core/Handles","../../../../core/watchUtils","../../../../core/accessorSupport/decorators","../../../../geometry/support/coordsUtils","../../../../symbols/SimpleMarkerSymbol","./drawUtils","./GraphicMover"],function(e,t,i,r,o,n,s,a,h,p,c,l,d,v,y){var u=function(){function e(e,t,i){this.graphic=e,this.mover=t,this.selected=i,this.type="reshape-start"}return e}(),f=function(){function e(e,t,i){this.graphic=e,this.mover=t,this.selected=i,this.type="reshape"}return e}(),m=function(){function e(e,t,i){this.graphic=e,this.mover=t,this.selected=i,this.type="reshape-stop"}return e}(),g=function(){function e(e,t,i){this.mover=e,this.dx=t,this.dy=i,this.type="move-start"}return e}(),_=function(){function e(e,t,i){this.mover=e,this.dx=t,this.dy=i,this.type="move"}return e}(),x=function(){function e(e,t,i){this.mover=e,this.dx=t,this.dy=i,this.type="move-stop"}return e}(),b=function(){function e(e){this.added=e,this.type="vertex-select"}return e}(),G=function(){function e(e){this.removed=e,this.type="vertex-deselect"}return e}(),S=function(){function e(e,t,i){this.added=e,this.graphic=t,this.oldGraphic=i,this.type="vertex-add"}return e}(),w=function(){function e(e,t,i){this.removed=e,this.graphic=t,this.oldGraphic=i,this.type="vertex-remove"}return e}(),M={removeVertex:["Backspace","Delete"]};return function(e){function t(t){var i=e.call(this,t)||this;return i._handles=new h,i._mover=null,i._viewHandles=new h,i._totalDx=0,i._totalDy=0,i.callbacks={onReshapeStart:function(e){},onReshape:function(e){},onReshapeStop:function(e){},onMoveStart:function(e){},onMove:function(e){},onMoveStop:function(e){}},i.graphic=null,i.handleGraphics=[],i.handleHoverSymbol=new d({style:"circle",size:8,color:[33,205,255],outline:{color:[0,12,255],width:1}}),i.handleSelectionSymbol=new d({style:"circle",size:8,color:[255,255,255],outline:{color:[0,12,255],width:1}}),i.handleSymbol=new d({style:"circle",size:6,color:[33,205,255],outline:{color:[0,12,255],width:1}}),i.layer=null,i.midpointGraphics=[],i.midpointSymbol=new d({style:"circle",size:6,color:[200,200,200],outline:{color:[100,100,100],width:1}}),i.enableMidpoints=!0,i.selectedHandles=[],i.view=null,i}return i(t,e),t.prototype.initialize=function(){var e=this;this._setup(),this._handles.add([p.whenOnce(this,"view.ready",function(){e.view&&e.view.map&&e.view.map.add(e.layer),e._viewHandles.add([e.view.on("key-down",function(t){return e._keyDownHandler(t)})])}),p.pausable(this,"graphic",function(){return e.refresh()}),p.pausable(this,"layer",function(t,i){i&&(e._clearSelection(),e._resetGraphics(i)),e.refresh()}),p.pausable(this,"enableMidpoints",function(){e.refresh()})])},t.prototype.destroy=function(){this._reset(),this._mover&&this._mover.destroy(),this._mover=null,this._handles.removeAll(),this._handles=null,this._viewHandles.removeAll(),this._viewHandles=null},Object.defineProperty(t.prototype,"state",{get:function(){var e=!!this.get("view.ready"),t=!(!this.get("graphic")||!this.get("layer"));return e&&t?"active":e?"ready":"disabled"},enumerable:!0,configurable:!0}),t.prototype.isUIGraphic=function(e){var t=[];return this.graphic&&t.push(this.graphic),t.concat(this.handleGraphics,this.midpointGraphics),t.length&&t.indexOf(e)>-1},t.prototype.refresh=function(){this._reset(),this._setup()},t.prototype.reset=function(){this.graphic=null},t.prototype.clearSelection=function(){this._clearSelection()},t.prototype.removeSelectedHandles=function(){this.selectedHandles.length&&this._removeVertex(this.selectedHandles)},t.prototype._setup=function(){this.graphic&&this.layer&&(this._setupGraphics(),this._setupMover())},t.prototype._reset=function(){this._clearSelection(),this._resetGraphicStateVars(),this._resetGraphics(),this._mover&&this._mover.destroy(),this._mover=null,this.view.container.style.cursor="default"},t.prototype._resetGraphicStateVars=function(){this._totalDx=0,this._totalDy=0},t.prototype._resetGraphics=function(e){var t=e||this.layer;t&&(t.removeMany(this.handleGraphics),t.removeMany(this.midpointGraphics)),this.handleGraphics.forEach(function(e){return e.destroy()}),this.midpointGraphics.forEach(function(e){return e.destroy()}),this._set("handleGraphics",[]),this._set("midpointGraphics",[]),this._set("selectedHandles",[])},t.prototype._setupGraphics=function(){var e=this.graphic.geometry,t=l.geometryToCoordinates(e.clone());if("polygon"===e.type)for(var i=0,r=t;i<r.length;i++){var o=r[i],n=o[o.length-1];o[0][0]===n[0]&&o[0][1]===n[1]&&o.length>2&&o.pop()}this._set("handleGraphics",this._createHandleGraphics(t)),this.enableMidpoints&&this._set("midpointGraphics",this._createMidpointGraphics(t)),this._saveRelatedIndices(this.handleGraphics),this.layer.addMany(this.midpointGraphics),this.layer.addMany(this.handleGraphics)},t.prototype._createHandleGraphics=function(e){for(var t=[],i=0,r=e;i<r.length;i++)for(var s=r[i],a=e.indexOf(s),h=0,p=s;h<p.length;h++){var c=p[h],l=s.indexOf(c),d=c[0],v=c[1];t.push(new n({geometry:new o.Point({x:d,y:v,spatialReference:this.view.spatialReference}),symbol:this.handleSymbol,attributes:{pathIndex:a,pointIndex:l}}))}return t},t.prototype._createMidpointGraphics=function(e){for(var t=[],i=0,r=e;i<r.length;i++)for(var s=r[i],a=e.indexOf(s),h=0,p=s;h<p.length;h++){var c=p[h],d=s.indexOf(c),v=c[0],y=c[1],u=s[d+1]?d+1:0;if("polyline"!==this.graphic.geometry.type||0!==u){var f=s[u],m=f[0],g=f[1],_=l.getMidpoint([v,y],[m,g]),x=_[0],b=_[1];t.push(new n({geometry:new o.Point({x:x,y:b,spatialReference:this.view.spatialReference}),symbol:this.midpointSymbol,attributes:{pathIndex:a,pointIndexStart:d,pointIndexEnd:u}}))}}return t},t.prototype._saveRelatedIndices=function(e){for(var t=0,i=e;t<i.length;t++){for(var r=i[t],o=e.indexOf(r),n=[],s=r.geometry,a=s.x,h=s.y,p=0,c=e;p<c.length;p++){var l=c[p],d=e.indexOf(l);if(o!==d){var v=l.geometry,y=v.x,u=v.y;a===y&&h===u&&n.push(d)}}r.attributes.relatedGraphicIndices=n}},t.prototype._setupMover=function(){var e=this;this._mover=new y({enableMoveAllGraphics:!1,graphics:[].concat(this.handleGraphics,this.graphic,this.midpointGraphics),view:this.view,callbacks:{onGraphicClick:function(t){return e._onGraphicClickCallback(t)},onGraphicMoveStart:function(t){return e._onGraphicMoveStartCallback(t)},onGraphicMove:function(t){return e._onGraphicMoveCallback(t)},onGraphicMoveStop:function(t){return e._onGraphicMoveStopCallback(t)},onGraphicPointerOver:function(t){return e._onGraphicPointerOverCallback(t)},onGraphicPointerOut:function(t){return e._onGraphicPointerOutCallback(t)}}})},t.prototype._onGraphicClickCallback=function(e){var t=e.graphic;if(t===this.graphic)return void this.clearSelection();if(this.midpointGraphics.indexOf(t)>-1){if(e.viewEvent.stopPropagation(),2===e.viewEvent.button)return;var i=this.graphic.clone();this._createHandleFromMidpoint(t),this.refresh(),this._emitVertexAddEvent([t],i)}else this.handleGraphics.indexOf(t)>-1&&(e.viewEvent.stopPropagation(),2===e.viewEvent.button?this._removeVertex(t):(e.viewEvent.native.ctrlKey||this._clearSelection(),-1===this.selectedHandles.indexOf(t)?this._addToSelection(t):this._removeFromSelection(t,!0)))},t.prototype._onGraphicMoveStartCallback=function(e){var t=e.graphic;if(this._resetGraphicStateVars(),t===this.graphic){var i=e.dx,r=e.dy;return this.handleGraphics.forEach(function(e){return e.visible=!1}),this.midpointGraphics.forEach(function(e){return e.visible=!1}),this._clearSelection(),void this._emitMoveStartEvent(i,r)}this.midpointGraphics.indexOf(t)>-1?(this._clearSelection(),this._createHandleFromMidpoint(t),this._addToSelection(t)):-1===this.selectedHandles.indexOf(t)&&(this._clearSelection(),this._addToSelection(t)),this._emitReshapeStartEvent(t)},t.prototype._onGraphicMoveCallback=function(e){var t=e.graphic,i=e.dx,r=e.dy;this._totalDx+=i,this._totalDy+=r,t===this.graphic?this._emitMoveEvent(i,r):(this._syncGeometryAfterHandleMove(t,i,r),this._emitReshapeEvent(t))},t.prototype._onGraphicMoveStopCallback=function(e){var t=e.graphic,i=e.dx,r=e.dy;if(this._totalDx+=i,this._totalDy+=r,t===this.graphic){for(var o=0,n=[].concat(this.handleGraphics,this.midpointGraphics);o<n.length;o++){n[o].visible=!0}this._syncGraphicsWithGeometry(),this._emitMoveStopEvent()}else this._syncGeometryAfterHandleMove(t,i,r),this.midpointGraphics.indexOf(t)>-1&&this.refresh(),this._emitReshapeStopEvent(t);this._resetGraphicStateVars()},t.prototype._syncGraphicsWithGeometry=function(){var e=this.graphic.geometry,t="polyline"===e.type?e.paths:e.rings;this._updateHandleGraphicLocations(t),this._updateMidpointGraphicLocations(t)},t.prototype._updateHandleGraphicLocations=function(e){for(var t=0,i=this.handleGraphics;t<i.length;t++){var r=i[t],n=r.attributes,s=n.pathIndex,a=n.pointIndex,h=e[s][a],p=h[0],c=h[1];r.set("geometry",new o.Point(p,c,this.view.spatialReference))}},t.prototype._updateMidpointGraphicLocations=function(e){for(var t=0,i=this.midpointGraphics;t<i.length;t++){var r=i[t],n=r.attributes,s=n.pathIndex,a=n.pointIndexStart,h=n.pointIndexEnd,p=e[s][a],c=p[0],d=p[1],v=e[s][h],y=v[0],u=v[1],f=l.getMidpoint([c,d],[y,u]),m=f[0],g=f[1];r.geometry=new o.Point({x:m,y:g,spatialReference:this.view.spatialReference})}},t.prototype._syncGeometryAfterHandleMove=function(e,t,i){var r=this.graphic.geometry.clone(),o="polyline"===r.type?r.paths:r.rings,n=e.attributes,s=n.pathIndex,a=n.pointIndex,h=e.geometry,p=h.x,c=h.y,l=o[s].length-1;if(o[s][a]=[p,c],"polygon"===r.type&&(0===a?o[s][l]=[p,c]:a===l&&(o[s][0]=[p,c])),this.handleGraphics.indexOf(e)>-1){for(var d=e.attributes.relatedGraphicIndices,y=0,u=d;y<u.length;y++){var f=u[y],m=this.handleGraphics[f],g=m.attributes,_=g.pathIndex,x=g.pointIndex;o[_][x]=[p,c],m.geometry=e.geometry}for(var b=0,G=this.selectedHandles;b<G.length;b++){var S=G[b];if(S!==e){var w=S.attributes,M=w.pathIndex,H=w.pointIndex,k=w.relatedGraphicIndices,E=v.move(S.geometry,t,i,this.view),O=o[M].length-1;o[M][H]=[E.x,E.y],S.geometry=E,"polygon"===r.type&&(0===H?o[M][O]=[E.x,E.y]:H===O&&(o[M][0]=[E.x,E.y]));for(var I=0,R=k;I<R.length;I++){var f=R[I],m=this.handleGraphics[f],V=m.attributes,D=V.pathIndex,C=V.pointIndex;o[D][C]=[E.x,E.y],m.geometry=E}}}this._updateMidpointGraphicLocations(o)}this.graphic.geometry=r},t.prototype._onGraphicPointerOverCallback=function(e){var t=e.graphic;this.handleGraphics.indexOf(t)>-1&&-1===this.selectedHandles.indexOf(t)&&(t.symbol=this.handleHoverSymbol),"pointer"!==this.view.container.style.cursor&&(this.view.container.style.cursor="pointer")},t.prototype._onGraphicPointerOutCallback=function(e){var t=e.graphic;this.handleGraphics.indexOf(t)>-1&&-1===this.selectedHandles.indexOf(t)&&(t.symbol=this.handleSymbol),"default"!==this.view.container.style.cursor&&(this.view.container.style.cursor="default")},t.prototype._createHandleFromMidpoint=function(e){var t=this.graphic.geometry.clone(),i=e.attributes,r=i.pathIndex,o=i.pointIndexStart,n=i.pointIndexEnd,s=e.geometry,a=s.x,h=s.y,p=0===n?o+1:n;("polyline"===t.type?t.paths:t.rings)[r].splice(p,0,[a,h]),e.attributes={pathIndex:r,pointIndex:p,relatedGraphicIndices:[]},this.graphic.geometry=t},t.prototype._removeHandles=function(e){var t=this.graphic.geometry.clone(),i="polygon"===t.type?t.rings:t.paths;e instanceof n&&(e=[e]);for(var r=0,o=e;r<o.length;r++){var s=o[r],a=s.geometry,h=a.x,p=a.y;for(var c in i){var l=i[c];for(var d in l){var v=l[d],y=v[0],u=v[1];h===y&&p===u&&i[c].splice(Number(d),1)}}}if("polygon"===t.type)for(var f=0,m=i;f<m.length;f++){var g=m[f],_=g[0],h=_[0],p=_[1],x=g[g.length-1],b=x[0],G=x[1];(1===g.length||g.length<3&&h===b&&p===G)&&i.splice(i.indexOf(g),1),g.length>2&&(h!==b||p!==G)&&g.push(g[0])}else for(var S=0,w=i;S<w.length;S++){var M=w[S];1===M.length&&i.splice(i.indexOf(M),1)}this.graphic.geometry=t},t.prototype._addToSelection=function(e){e instanceof n&&(e=[e]);for(var t=0,i=e;t<i.length;t++){i[t].symbol=this.handleSelectionSymbol}this._set("selectedHandles",this.selectedHandles.concat(e)),this._emitSelectEvent(e)},t.prototype._removeFromSelection=function(e,t){var i=t?this.handleHoverSymbol:this.handleSymbol;e instanceof n&&(e=[e]);for(var r=0,o=e;r<o.length;r++){var s=o[r];this.selectedHandles.splice(this.selectedHandles.indexOf(s),1),s.symbol=i}this._emitDeselectEvent(e)},t.prototype._clearSelection=function(){if(this.selectedHandles.length){for(var e=this.selectedHandles,t=0,i=this.selectedHandles;t<i.length;t++){var r=i[t];r&&(r.symbol=this.handleSymbol)}this._set("selectedHandles",[]),this._emitDeselectEvent(e)}},t.prototype._keyDownHandler=function(e){M.removeVertex.indexOf(e.key)>-1&&!e.repeat&&this.selectedHandles.length&&this._removeVertex(this.selectedHandles)},t.prototype._removeVertex=function(e){if(!("polygon"===this.graphic.geometry.type&&this.handleGraphics.length<4||this.handleGraphics.length<3)){var t=this.graphic.clone();e instanceof n&&(e=[e]),this._removeHandles(e),this.refresh(),this._emitVertexRemoveEvent(e,t)}},t.prototype._emitMoveStartEvent=function(e,t){var i=new g(this.graphic,e,t);this.emit("move-start",i),this.callbacks.onMoveStart&&this.callbacks.onMoveStart(i)},t.prototype._emitMoveEvent=function(e,t){var i=new _(this.graphic,e,t);this.emit("move",i),this.callbacks.onMove&&this.callbacks.onMove(i)},t.prototype._emitMoveStopEvent=function(){var e=new x(this.graphic,this._totalDx,this._totalDy);this.emit("move-stop",e),this.callbacks.onMoveStop&&this.callbacks.onMoveStop(e)},t.prototype._emitReshapeStartEvent=function(e){var t=new u(this.graphic,e,this.selectedHandles);this.emit("reshape-start",t),this.callbacks.onReshapeStart&&this.callbacks.onReshapeStart(t)},t.prototype._emitReshapeEvent=function(e){var t=new f(this.graphic,e,this.selectedHandles);this.emit("reshape",t),this.callbacks.onReshape&&this.callbacks.onReshape(t)},t.prototype._emitReshapeStopEvent=function(e){var t=new m(this.graphic,e,this.selectedHandles);this.emit("reshape-stop",t),this.callbacks.onReshapeStop&&this.callbacks.onReshapeStop(t)},t.prototype._emitSelectEvent=function(e){var t=new b(e);this.emit("select",t),this.callbacks.onVertexSelect&&this.callbacks.onVertexSelect(t)},t.prototype._emitDeselectEvent=function(e){var t=new G(e);this.emit("deselect",t),this.callbacks.onVertexDeselect&&this.callbacks.onVertexDeselect(t)},t.prototype._emitVertexAddEvent=function(e,t){var i=new S(e,this.graphic,t);this.emit("vertex-add",i),this.callbacks.onVertexAdd&&this.callbacks.onVertexAdd(i)},t.prototype._emitVertexRemoveEvent=function(e,t){var i=new w(e,this.graphic,t);this.emit("vertex-remove",i),this.callbacks.onVertexRemove&&this.callbacks.onVertexRemove(i)},r([c.property()],t.prototype,"callbacks",void 0),r([c.property()],t.prototype,"graphic",void 0),r([c.property({readOnly:!0})],t.prototype,"handleGraphics",void 0),r([c.property()],t.prototype,"handleHoverSymbol",void 0),r([c.property()],t.prototype,"handleSelectionSymbol",void 0),r([c.property()],t.prototype,"handleSymbol",void 0),r([c.property()],t.prototype,"layer",void 0),r([c.property({readOnly:!0})],t.prototype,"midpointGraphics",void 0),r([c.property()],t.prototype,"midpointSymbol",void 0),r([c.property()],t.prototype,"enableMidpoints",void 0),r([c.property({readOnly:!0})],t.prototype,"selectedHandles",void 0),r([c.property({dependsOn:["view.ready","graphic","layer"],readOnly:!0})],t.prototype,"state",null),r([c.property()],t.prototype,"view",void 0),t=r([c.subclass("esri.views.2d.draw.support.Reshape")],t)}(c.declared(s,a))});