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 TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";

import UserManager from "../Manager/UserManager";

import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

import StockManager from "./StockManager";


const {ccclass, property} = cc._decorator;

enum AddSub{
    Add,
    Sub
}
@ccclass
export default class BuyStockBox extends cc.Component {
    @property(cc.Node)
    stockName:cc.Node = null 
    @property(cc.Node)
    huafei: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
    stockSum:number = 0
    onLoad() {
        this.openTouchEvent(this.btn_add)  
        this.openTouchEvent(this.btn_reduce) 
    }
    initBoxContent(stockConfig){
        this.curPrice = stockConfig.curPrice
        this.curStockName = stockConfig.stockName
        let singConfig = StockManager.getSingleCompanyConfig(stockConfig.stockName)
        this.stockSum = singConfig.stockSum
        let myStockHistoryData = User.getMyStockData()
        let curStockHistoryData = myStockHistoryData[this.curStockName]
        let historyProssess = (curStockHistoryData?curStockHistoryData.prossess:0)

        console.log( this.curPrice , this.curStockName ,this.stockSum,historyProssess,'this.curStockName +++++++===')
        this.stockName.getComponent(cc.Label).string = stockConfig.stockName
        this.huafei.getComponent(cc.Label).string = Common5.getNumberChangeHanzi(stockConfig.curPrice,'1',1)+""
        this.amount.getComponent(cc.Label).string = "1"
        if(this.stockSum - this.curAmount - historyProssess <= 0 ){
            this.leftStockNum.getComponent(cc.Label).string = "还剩(0股)"
            this.amount.getComponent(cc.Label).string = "0"
            this.huafei.getComponent(cc.Label).string = '0'
            this.curAmount = 0

        }else{
            this.leftStockNum.getComponent(cc.Label).string = "还剩("+(this.stockSum - this.curAmount - historyProssess)+"股)"
        }
        //this.leftStockNum.getComponent(cc.Label).string = "还剩("+(this.stockSum - this.curAmount - historyProssess)+"股)"
    }

    onBtnClose(){
        //Common5.playEffect("sound/按键点击")
        this.node.active = false
     
   

    }
    //购买
    onBtnSure(){
        //Common5.playEffect("sound/按键点击")
  
        if(this.curAmount == 0){
            //Common5.showTips_customTime('当前股票卖完了换一支股票吧',1)
            PrefabManage.showTextTips('当前股票卖完了换一支股票吧')
            return
        }
        let allMoney = User.getMoney()
        let expendMoney = this.curAmount*this.curPrice
        console.log("钱总数==花钱==",allMoney,expendMoney)



        if(expendMoney > allMoney){
            //Common5.showTips_customTime('余额不足挣钱去吧',1)
            PrefabManage.showTextTips('余额不足挣钱去吧')
        }else{
            UserManager.subMoney(expendMoney)
            this.refreshMyStockStorageData()
        }
        this.node.active = false

    }
    refreshLabelShow(leftAmount){
        this.huafei.getComponent(cc.Label).string = Common5.getNumberChangeHanzi(this.curAmount*this.curPrice,'1',1)+""


        console.log(this.curAmount,'this.curAmount+++=')
        console.log(this.curPrice,'this.curPrice+++=')

        let bb = Common5.getNumberChangeHanzi(this.curAmount,'1')
        this.amount.getComponent(cc.Label).string = bb+""
        
        let aa = Common5.getNumberChangeHanzi(leftAmount,'1')
        this.leftStockNum.getComponent(cc.Label).string = "还剩("+ aa +"股)"
    }
    //刷新我的股票购买数据
    refreshMyStockStorageData(){
        let myStockHistoryData = User.getMyStockData()
        console.log("已存在数据==",myStockHistoryData)
        let curStockHistoryData = myStockHistoryData[this.curStockName]
        let newProssess = this.curAmount
        //已经购买过该股票
        if(curStockHistoryData){
            let historyBuyPrice = curStockHistoryData.buyPrice
            let historyProssess = curStockHistoryData.prossess

            newProssess = historyProssess + this.curAmount
            let newBuyPrice = Math.floor((historyBuyPrice*historyProssess + this.curPrice*this.curAmount)/newProssess)
            let buyStockTab = {
                stockName:this.curStockName,
                buyPrice:newBuyPrice,
                prossess:newProssess
            }
            User.setMyStockData(buyStockTab)
        //首次购买该股票
        }else{
            newProssess = this.curAmount
            let buyStockTab = {
                stockName:this.curStockName,
                buyPrice:this.curPrice,
                prossess:newProssess
            }
            User.setMyStockData(buyStockTab)
        }
        if(this.curStockName == '巨富集团' && newProssess >= this.stockSum){
            //完成任务533
            let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
            let mainId = mainTaskInfo.Id
            if(mainId == MainTaskIdEnum.MainTask_530){
        
                //EventMgr.emitEvent_custom(ryw_Event.RefreshUnLockGame);
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_530)
                this.node.parent.removeFromParent()
                this.node.parent.destroy()
                
            }
        }


        EventMgr.emitEvent_custom(ryw_Event.updateStockView);
    }

    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)
    }

    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.curPrice >= User.getMoney()){
                //this.curAmount = Math.floor(User.getMoney() / this.curPrice)
            }else{
                
            }  


            let myStockHistoryData = User.getMyStockData()
            let curStockHistoryData = myStockHistoryData[this.curStockName]
            let historyProssess = curStockHistoryData?curStockHistoryData.prossess:0
            console.log("this.stockSum=",this.stockSum)
            console.log("this.curAmount=",this.curAmount)
            console.log("historyProssess=",historyProssess)
            let leftAmount = this.stockSum - this.curAmount - historyProssess
           
            if(leftAmount<=0){
                leftAmount = 0
            }

        
            if(this.stockSum-historyProssess<=0){
                this.curAmount = 0
            }
            if(this.curAmount>=this.stockSum){
                this.curAmount = this.stockSum
            }
            this.refreshLabelShow(leftAmount)
        }
        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(){

  



        let myStockHistoryData = User.getMyStockData()
        let curStockHistoryData = myStockHistoryData[this.curStockName]
        let historyProssess = curStockHistoryData?curStockHistoryData.prossess:0

        if(  this.stockSum*this.curPrice >= User.getMoney()){
            this.curAmount = Math.floor(User.getMoney() / this.curPrice)

            this.curAmount = this.stockSum-historyProssess
            if(this.curAmount<=0){
                this.curAmount = 0
            }
            
        }else{
            this.curAmount = this.stockSum-historyProssess
            if(this.curAmount<=0){
                this.curAmount = 0
            }
        }  

        let leftAmount = this.stockSum - this.curAmount - historyProssess
        if(leftAmount<=0){
            leftAmount = 0
    
        }
        console.log("this.stockSum=",this.stockSum)
        console.log("this.curAmount=",this.curAmount)
        console.log("historyProssess=",historyProssess)


        this.refreshLabelShow(leftAmount)
    }

    buyMinClick(){
        
  

        this.curAmount = 1
        let myStockHistoryData = User.getMyStockData()
        let curStockHistoryData = myStockHistoryData[this.curStockName]
        let historyProssess = curStockHistoryData?curStockHistoryData.prossess:0
        let leftAmount = this.stockSum - this.curAmount - historyProssess
        if(leftAmount<=0){
            leftAmount = 0
            this.curAmount = 0
        }
        this.refreshLabelShow(leftAmount)
    }


}