"use strict";var _assets=require("./assets"),_constant=require("../../../../utils/constant"),_luckymoneyService=require("../../../../services/luckymoney-service"),_luckymoney=require("../../../../utils/luckymoney"),_luckymoneyStore=require("../../../../store/luckymoney-store"),velX=1.8,velRatio=Math.tan(60*Math.PI/180),config={maxScale:2,baseWidth:50,baseHeight:50,velX:velX,velY:velX*velRatio},area=14400;function getRandom(t,e){if(t>e){var n=[e,t];t=n[0],e=n[1]}var i=Math.random()*(e-t)+t;return Number(i.toFixed(2))}Component({properties:{rainingRedPacketID:{type:String,value:"",observer:"onRainingIdChange"},sdkInstance:{type:Object,value:null},duration:{type:Number,value:_constant.RedPacketRainDuration}},data:{start:!1,visible:!1,trailerCount:5,rainCount:30,hongBaoCount:0,hongBaoActive:!1},observers:{sdkInstance:function(t){t&&(this.sdkInstance=t,this.sdkInstanceId=(null==t?void 0:t.uuid)||t,this.store=t.store,this.luckymoneyStore=(0,_luckymoneyStore.getLuckymoneyStore)(this.sdkInstanceId))}},lifetimes:{detached:function(){this.clearTimers()}},methods:{getSdkInstanceId:function(){var t,e,n;return(null===(t=this.properties)||void 0===t||null===(e=t.sdkInstance)||void 0===e?void 0:e.uuid)||(null===(n=this.properties)||void 0===n?void 0:n.sdkInstance)},isLogin:function(){var t,e,n;return 2===((null===(t=this.properties)||void 0===t||null===(e=t.sdkInstance)||void 0===e||null===(n=e.store)||void 0===n?void 0:n.get("mainInfo"))||{}).mode},clearTimers:function(){var t;if(clearInterval(this.trailerTimer),clearInterval(this.rainTimer),this.requestID&&null!==(t=this.canvasNode)&&void 0!==t&&t.cancelAnimationFrame)try{this.canvasNode.cancelAnimationFrame(this.requestID)}catch(t){}},onRainingIdChange:function(t){t&&this.restart()},restart:function(){var t=this;this.clearTimers(),this.setData({start:!0,visible:!1,trailerCount:5,hongBaoCount:0,rainCount:this.data.duration}),this.trailerTimer=setInterval(function(){var e=t.data.trailerCount-1;t.setData({trailerCount:e}),e<=0&&(clearInterval(t.trailerTimer),t.startRain())},1e3)},startRain:function(){var t=this;this.setData({start:!1,visible:!0}),setTimeout(function(){t.initCanvas()},100),this.rainTimer=setInterval(function(){var e=t.data.rainCount-1;t.setData({rainCount:e}),e<=0&&t.stopRain()},1e3)},stopRain:function(){var t,e=null===(t=this.data)||void 0===t?void 0:t.hongBaoCount;this.clearTimers(),this.setData({visible:!1}),this.onRainEnd(e),this.triggerEvent("end",{count:e})},onRainEnd:function(t){var e=this.data.rainingRedPacketID;if(e){var n,i,a,o,s=null===(n=this.properties)||void 0===n||null===(i=n.sdkInstance)||void 0===i?void 0:i.uuid;null===(a=(0,_luckymoneyService.getLuckymoneyService)(s))||void 0===a||null===(o=a.stopRain)||void 0===o||o.call(a,e,t)}},onHongBaoClick:function(){var t,e=this,n=this.data.rainingRedPacketID,i=this.getSdkInstanceId(),a=this.luckymoneyStore||(0,_luckymoneyStore.getLuckymoneyStore)(i),o=(null==a||null===(t=a.state)||void 0===t?void 0:t.userStatusMap)||{},s=(null==o?void 0:o[n])||_luckymoney.LuckyMoneyResult.UNATTENDED,r=s!==_luckymoney.LuckyMoneyResult.UNATTENDED&&s!==_luckymoney.LuckyMoneyResult.ERROR;this.isLogin()&&r?(this.setData({hongBaoCount:this.data.hongBaoCount+1,hongBaoActive:!0}),setTimeout(function(){e.setData({hongBaoActive:!1})},100)):wx.showToast({title:"您未在红包雨开启前完成参与条件，无法参与本轮抢红包",icon:"none"})},initCanvas:function(){var t=this;this.createSelectorQuery().select("#rain-canvas").fields({node:!0,size:!0}).exec(function(e){if(e[0]&&e[0].node){var n=e[0].node,i=n.getContext("2d"),a=wx.getSystemInfoSync().pixelRatio;n.width=e[0].width*a,n.height=e[0].height*a,i.scale(a,a),t.canvasNode=n;var o=n.createImage();o.src=_assets.base64Url,o.onload=function(){t.execAnimation(n,i,o,e[0].width,e[0].height)}}})},execAnimation:function(t,e,n,i,a){var o=this,s=[],r=i*a/area;!function c(){var u,l,d,v,h,m,g;s.length<r&&s.push((u=getRandom(1,config.maxScale),l=config.baseWidth*u,d=config.baseHeight*u,v=config.velX*u,h=config.velY*u,m=getRandom(a/velRatio,i+a/velRatio),g=-1*getRandom(0,a),{step:function(){g+=h,((m-=v)<0-l||g>a+d)&&(m=getRandom(a/velRatio,i+a/velRatio),g=-1*getRandom(0,a))},draw:function(){e.drawImage(n,m,g,l,d)}})),e.clearRect(0,0,i,a),s.forEach(function(t){t.step(),t.draw()}),o.requestID=t.requestAnimationFrame(c)}()}}});