import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";

import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import UserManager from "../Manager/UserManager";


const {ccclass, property} = cc._decorator;
enum AddSub{
    Add,
    Sub
}
@ccclass
export default class SaleStockBox extends cc.Component {
    @property(cc.Node)
    stockName:cc.Node = null 
    @property(cc.Node)
    saleGet:cc.Node = null
    @property(cc.Node)
    shouXuFei:cc.Node = null
    @property(cc.Node)
    yingLiMoney:cc.Node = null
    @property(cc.Node)
    amount:cc.Node = null
    @property(cc.Node)
    leftStockNum:cc.Node = null

    @property(cc.Node)
    btn_reduce:cc.Node = null 
    @property(cc.Node)
    btn_add:cc.Node = null 

    curStockName:string = ""
    curAmount:number = 1
    curPrice:number = 0
    historyBuyPrice:number = 0
    historyProssess:number = 0

    onLoad() {
        this.openTouchEvent(this.btn_add)  
        this.openTouchEvent(this.btn_reduce) 
    }
    openTouchEvent(node){
        node.on(cc.Node.EventType.TOUCH_START,this.touchStartNode,this)
        node.on(cc.Node.EventType.TOUCH_MOVE,this.touchMoveNode,this)
        node.on(cc.Node.EventType.TOUCH_CANCEL,this.touchEndNode,this)
        node.on(cc.Node.EventType.TOUCH_END,this.touchEndNode,this)
    }
    initBoxContent(stockConfig){
        this.curPrice = stockConfig.curPrice
        this.curStockName = stockConfig.stockName

        this.stockName.getComponent(cc.Label).string = stockConfig.stockName
        this.saleGet.getComponent(cc.Label).string = Common5.getNumberChangeHanzi(this.curPrice,'1',1)+""

        let myStockHistoryData = User.getMyStockData()
        let curStockHistoryData = myStockHistoryData[this.curStockName]
        //购买价
        this.historyBuyPrice = curStockHistoryData.buyPrice
        //拥有数量
        this.historyProssess = curStockHistoryData.prossess

        let yingLi = this.curPrice - this.historyBuyPrice
        if(yingLi >= 0){
            //this.yingLiMoney.color = new cc.Color().fromHEX("#D20D00")
            this.yingLiMoney.getComponent(cc.Label).string = "盈利:" + Common5.getNumberChangeHanzi(yingLi,'1',1)
        }else{
            // this.yingLiMoney.color = new cc.Color().fromHEX("#03A239")
            this.yingLiMoney.getComponent(cc.Label).string = "亏损:" + Common5.getNumberChangeHanzi(Math.abs(yingLi),'1',1)
        }
        this.leftStockNum.getComponent(cc.Label).string = "还剩:"+(this.historyProssess-1)+"股"
    }
    onBtnClose(){
        //Common5.playEffect("sound/按键点击")
        this.node.active = false
 
      
    }
    refreshLabelShow(){
        this.saleGet.getComponent(cc.Label).string = Common5.getNumberChangeHanzi(this.curPrice*this.curAmount,'1',1)+""
        let bb = Common5.getNumberChangeHanzi(this.curAmount,'1')

        this.amount.getComponent(cc.Label).string = bb+""

        let yingLi = this.curPrice - this.historyBuyPrice
        if(yingLi >= 0){
            //this.yingLiMoney.color = new cc.Color().fromHEX("#D20D00")
            this.yingLiMoney.getComponent(cc.Label).string = "盈利:" + Common5.getNumberChangeHanzi(yingLi*this.curAmount,'1',1)
        }else{
            //this.yingLiMoney.color = new cc.Color().fromHEX("#03A239")
            this.yingLiMoney.getComponent(cc.Label).string = "亏损:" +  Common5.getNumberChangeHanzi(Math.abs(yingLi*this.curAmount),'1',1)
        }
        this.leftStockNum.getComponent(cc.Label).string = "还剩:"+(this.historyProssess-this.curAmount)+"股"
    }
    //出售
    onBtnSale(){
        //Common5.playEffect("sound/按键点击")
        let shuiLv = 0.02
        let truthMoney = Math.floor(this.curPrice*this.curAmount*(1-shuiLv))
        this.refreshMyStockStorageData() 
        UserManager.addMoney(truthMoney,this.node.parent)
      
 
        this.node.active = false
    }
    //刷新我的股票购买数据
    refreshMyStockStorageData(){
        let myStockHistoryData = User.getMyStockData()
        console.log("已存在数据==",myStockHistoryData)
        let curStockHistoryData = myStockHistoryData[this.curStockName]
        let historyProssess = curStockHistoryData.prossess
        let newProssess = historyProssess - this.curAmount
        //全部卖出
        if(newProssess <= 0){
            delete myStockHistoryData[this.curStockName]
        }else{
            curStockHistoryData.prossess = newProssess
        }
        EventMgr.emitEvent_custom(ryw_Event.updateStockView);
    }
    touchStartNode(event){
        //Common5.playEffect("sound/按键点击")
        let target = event.target
        target.scale = 1.05
        if(target.name == 'btn_reduce'){
            this.addOrSubMoneyCommon(AddSub.Sub)
 
        }else if(target.name == 'btn_add'){
            this.addOrSubMoneyCommon(AddSub.Add)
 
           
        }
    }

    touchMoveNode(event){

    }

    addOrSubMoneyCommon(type){
        if(AddSub.Sub == type){
            this.curAmount -= 1 
        }else if(AddSub.Add == type){
            this.curAmount += 1 
        }
        
        let callFunc = ()=>{
            if(this.curAmount < 1){
                this.curAmount = 1
            }
            if(this.curAmount > this.historyProssess){
                this.curAmount = this.historyProssess
            }
            this.refreshLabelShow()
        }
        callFunc()

        cc.tween(this.node)
        .delay(0.3)
        .call(()=>{
            let random = Common5.getRandomNum(100,20000)
            if(AddSub.Sub == type){
                this.curAmount -= random
            }else if(AddSub.Add == type){
                this.curAmount += random
            }  
            callFunc()
        })
        .union()
        .repeatForever()
        .start()
    }

    touchEndNode(event){
        let target = event.target
        target.scale = 1.00
        this.node.stopAllActions()
    }
    
    buyMaxClick(){
        this.curAmount = this.historyProssess
        this.refreshLabelShow()
        
     
    }

    buyMinClick(){
        this.curAmount = 1
        this.refreshLabelShow()

     
    }
    
  
}