import { _decorator, Label, Node, Sprite, SpriteFrame } from 'cc';
import { ResKeeper } from '../../../framework/res/ResKeeper';
import { StringUtil } from '../../../framework/util/StringUtil';
import { MailData } from '../../data/MailData';
import { Framework } from '../../../framework/Framework';
import { ViewID } from '../../../framework/config/LayerConf';
import { LoginMgr } from '../../common/LoginManager';
import { GameEvent } from '../../data/GameEvent';
import { MailManager } from '../../manager/MailManager';
const { ccclass, property } = _decorator;

@ccclass('MailItem')
export class MailItem extends ResKeeper {
	@property({ type: Node, tooltip: "邮件状态" })
	awardBg: Node = null;

	@property({ type: Node, tooltip: "奖励节点" })
	awardNode: Node = null;

	@property({ type: Label, tooltip: "邮件标题" })
	titleTx: Label = null;

	@property({ type: Label, tooltip: "邮件时间" })
	timeTx: Label = null;

	@property({ type: Node, tooltip: "已读状态" })
	readIco: Node = null;
	@property({ type: Label, tooltip: "已读状态文字" })
	readIcoTx: Label = null;

	@property({ type: Node, tooltip: "附件" })
	awardTips: Node = null;
	@property({ type: Label, tooltip: "附件文字" })
	awardTipsTx: Label = null;

	@property({ type: Node, tooltip: "已读遮罩" })
	maskImg: Node = null;

	private data = null;
	protected onLoad() {
		this.readIcoTx.string = StringUtil.getLanguageData('已读');
		this.awardTipsTx.string = StringUtil.getLanguageData('附件');
	}

	refreshItem(data) {
		this.data = data;

		this.titleTx.string = MailManager.getMailText(data.title,true);

		this.maskImg.active = false;
		this.readIco.active = false;

		let date = new Date(data.time);
		let Y = date.getFullYear() + '-';
		let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '';
		this.timeTx.string = Y + M + D;

		if (data.award && data.award.size > 0) {
			this.awardTips.active = false;
			let showAward = MailManager.getShowAward(data.id)
			if (showAward) {
				this.awardBg.active = false;
				this.awardNode.active = true;
			} else {
				this.awardBg.active = true;
				this.awardNode.active = false;
				this.load('mail', 'texture/mail_3/spriteFrame', SpriteFrame, (res: SpriteFrame) => {
					this.awardBg.getComponent(Sprite).spriteFrame = res;
				})
			}
		} else {
			this.awardBg.active = true;
			this.awardNode.active = false;
			this.awardTips.active = false;
			this.load('mail', 'texture/mail_3/spriteFrame', SpriteFrame, (res: SpriteFrame) => {
				this.awardBg.getComponent(Sprite).spriteFrame = res;
			})
		}

		if (data.read == 1) {
			this.awardBg.active = true;
			this.awardNode.active = false;
			this.awardTips.active = false;
			this.readIco.active = true;
			this.maskImg.active = true;
			this.load('mail', 'texture/mail_2/spriteFrame', SpriteFrame, (res: SpriteFrame) => {
				this.awardBg.getComponent(Sprite).spriteFrame = res;
			})
		}
	}
	protected onDestroy() {
		super.onDestroy();
	}

	onClose() {

	}

	//UI事件处理
	private onTouchButton(event: Event) {
		let target: any = event.target;
		if (target.name == 'bg') {
			if (this.data.read == 1) {
				Framework.layer.open(ViewID.MailDetail, null, this.data);
				return;
			}
			if (this.data.award && this.data.award.size > 0) {
				Framework.layer.open(ViewID.MailDetail, null, this.data);
			} else {
				MailManager.sendReadMailMsg({ id: this.data.id },()=>{
					Framework.event.fireEvent(GameEvent.MailUpdate);
					Framework.layer.open(ViewID.MailDetail, null, this.data);
				})
			}
		}
	}
}


