// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import Common5 from "../../Platform/th/Common5";
import FangChanManager from "../Manager/FangChanManager";
import UserManager from "../Manager/UserManager";
import PrefabManage from "../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FangChanItem extends cc.Component {



    @property(cc.Label)
    fangName2: cc.Label = null;

    @property(cc.Label)
    fangName3: cc.Label = null;

    @property(cc.Label)
    buyLab: cc.Label = null;

    @property(cc.Label)
    saleLab: cc.Label = null;


    @property(cc.Node)
    saleNode: cc.Node = null;


    @property(cc.Node)
    buyNode: cc.Node = null;

    @property(cc.Label)
    buyPriceLab: cc.Label = null;

    @property(cc.Sprite)
    iconSpr: cc.Sprite = null;
    @property(cc.Node)
    redPoint: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    config = null
    buyPrice: number = 0
    start() {
        this.refrehView()
        EventMgr.onEvent_custom(ryw_Event.FangChanIncome, () => {
            this.refrehView()
        }, this);
    }

    refrehView() {
        this.node.getChildByName('buyBtn').getChildByName("noshop").active = false
        if (this.config) {
            let state = FangChanManager.getRoomState(this.config.roomId)
            if (state == 0) {
                this.saleNode.active = false
                this.buyNode.active = true
                this.node.getChildByName('buyBtn').active = true
                this.node.getChildByName('saleBtn').active = false
                if (UserManager.getCurMoney() - this.buyPrice >= 0) {
                    this.redPoint.active = true
                } else {
                    this.redPoint.active = false
                    this.node.getChildByName('buyBtn').getChildByName("noshop").active = true
                }
            } else {
                this.saleNode.active = true
                this.buyNode.active = false
                this.node.getChildByName('buyBtn').active = false
                this.node.getChildByName('saleBtn').active = true
                this.node.getChildByName('kuang').active = false
                //刷新收益
                let shouyi = FangChanManager.getInComeMoneyById(this.config.roomId)
                this.saleLab.string = '' + Common5.getNumberChangeHanzi(shouyi + this.config.buyPrice, '1', 4)

                if (this.config.name == '公寓' || this.config.name == '市区住房' || this.config.name == '高档大平层' || this.config.name == '欧式城堡') {
                    this.node.getChildByName('saleBtn').active = false
                    this.node.getChildByName('不可出售').active = true
                }
            }

        }
    }

    setDateView(config) {
        this.config = config


        this.buyPriceLab.string = Common5.getNumberChangeHanzi(this.config.buyPrice, '1', 0) + ''

        this.buyPrice = this.config.buyPrice
        this.refrehView()
        Common5.addUrlSprite_custom(this.config.iconUrl + this.config.name, this.iconSpr)

        this.fangName2.string = this.config.name
        this.fangName3.string = this.config.name
        this.buyLab.string = Common5.getNumberChangeHanzi(this.config.buyPrice, '1', 0) + ''
    }

    buyBtnClick() {
        if (UserManager.getCurMoney() - this.buyPrice >= 0) {
            UserManager.subMoney(this.buyPrice)
            FangChanManager.setRoomState(this.config.roomId, 1)
            this.refrehView()
            Common5.playRemoteAudioEffect('sound/diandiandian/升级音效')
        } else {
            PrefabManage.showTextTips('余额不足!')
        }
    }

    saleBtnClick() {

        let shouyi = FangChanManager.getInComeMoneyById(this.config.roomId)
        UserManager.addMoney(shouyi + this.config.buyPrice)

        FangChanManager.resetInComeMoneyById(this.config.roomId)
        FangChanManager.setRoomState(this.config.roomId, 0)
        this.refrehView()
    }

    // update (dt) {}
}