import Main from "../../main/Main";
import ViewObject from "../../main/ViewObject";
import { HttpStateType, ReveData } from "../../util/CHttp";
import GoodItem from "../common/GoodItem";
import Mail from "./Mail";
import MailItem from "./MailItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MailInfo extends cc.Component {

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

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

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

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

    @property(cc.Prefab)
    mGoodItem: cc.Prefab = null;

    @property(cc.Button)
    mBTReward: cc.Button = null;

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

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

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

    /**
     * 邮件内容
     */
    public mailItem:MailItem;
    private mail:Mail
    private main:Main
    public data:any;

    onLoad () {
        this.mMailNo.active = true
        this.mMailYes.active = false
    }
    start(){
        this.mail = this.node.getComponent(Mail)
        this.main = this.mail.main;
    }
    public setMail(mailItem:MailItem){
        this.mMailNo.active = false
        this.mMailYes.active = true
        this.mailItem = mailItem
        this.data = this.mailItem.data
        this.flush();
    }

    public flush(){
        this.mBTReward.interactable = !this.data.receive;
        if(this.data.receive){
            this.mBTLable.string = '已领取';
        }else{
            this.mBTLable.string = '领取';
        }
    }
    public getMailInfo(){
        this.mTitle.string = this.data.title;
        let msg = {
            mailId:this.data.id
        }
        this.main.gameHttp.sendJson('email/v1/info',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    this.data = reve.data;
                    this.initData();
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }

    private initData(){
        this.node.getComponent(Mail).mInfoNode.active = true
        this.mContent.string = this.data.content;
        this.mTime.string = this.data.date+'  '+this.data.time;

        // "appendix":"[{\"icon\":\"aa\",\"count\":1}]"
        this.mReward.destroyAllChildren()
        if(this.data.appendix){
            let goods = JSON.parse(this.data.appendix);
            for (let i = 0; i < goods.length; i++) {
                const element = goods[i];
                let node = cc.instantiate(this.mGoodItem);
                let gl = node.getComponent(GoodItem);
                gl.init(this.main,element);
                node.parent = this.mReward;
            }
        }

    }

    public onclickGet(){
        let msg = {
            mailId:this.data.id
        }
        this.main.gameHttp.sendJson('email/v1/receive',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    this.data.receive = true;
                    this.flush();
                    // this.main.showTips('获得各种奖励..');
                    this.main.showReward(reve)
                    this.mailItem.tipsIcon.active = false;
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }




}
