import { _decorator, instantiate, Label, Node, Prefab, RichText, ScrollView, tween, UIOpacity, v3 } from 'cc';
import { BaseView } from '../../../framework/layer/BaseView';
import { Framework } from '../../../framework/Framework';
import { StringUtil } from '../../../framework/util/StringUtil';
import { ViewID } from '../../../framework/config/LayerConf';
import { GameEvent } from '../../data/GameEvent';
import { MailData } from '../../data/MailData';
import { LoginMgr } from '../../common/LoginManager';
import List from '../../../framework/list/List';
import { CommonItem } from '../common/CommonItem';
import { MailManager } from '../../manager/MailManager';
const { ccclass, property } = _decorator;

@ccclass('MailDetail')
export class MailDetail extends BaseView {
	@property({ type: Label, tooltip: "关闭提示" })
	closeTips: Label = null;

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

	@property({ type: Label, tooltip: "发件人标题" })
	fromTitle: Label = null;

	@property({ type: Label, tooltip: "发件人文字" })
	fromTx: Label = null;

	@property({ type: Label, tooltip: "主题标题" })
	captionTitle: Label = null;

	@property({ type: Label, tooltip: "主题文字" })
	captionTx: Label = null;

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

	@property({ type: RichText, tooltip: "邮件内容" })
	msgTx: RichText = null;

	@property({ type: Node, tooltip: "附件背景" })
	awardsBg: Node = null;

	@property({ type: List, tooltip: "附件容器" })
	awardsSv: List = null;

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

	@property({ type: Node, tooltip: "附件领取标记" })
	getTips: Node = null;

	@property({ type: Node, tooltip: "领取按钮" })
	getBtn: Node = null;

	@property({ type: Label, tooltip: "领取按钮文字" })
	getBtnTx: Label = null;

	@property({ type: Node, tooltip: "删除按钮" })
	delBtn: Node = null;

	@property({ type: Label, tooltip: "删除按钮文字" })
	delBtnTx: Label = null;

	private data = null;
	onLoad() {
		super.onLoad();
		this.closeTips.string = StringUtil.getLanguageData('点击空白关闭');
		this.titleTx.string = StringUtil.getLanguageData('邮件详情');
		this.fromTitle.string = StringUtil.getLanguageData('来自');
		this.captionTitle.string = StringUtil.getLanguageData('主题');
		this.awardsTitle.string = StringUtil.getLanguageData('附件');
		this.getTips.getComponent(Label).string = StringUtil.getLanguageData('附件已领取');
		this.getBtnTx.string = StringUtil.getLanguageData('领取附件');
		this.delBtnTx.string = StringUtil.getLanguageData('删除邮件');
		this.closeTips.node.getComponent(UIOpacity).opacity = 0;
	}

	onDestroy() {
		super.onDestroy();
	}

	onOpen(data) {
		this.data = data;
		this.updateUI();

		tween(this.closeTips.node.getComponent(UIOpacity))
			.to(1, { opacity: 255 })
			.to(1.2, { opacity: 10 })
			.union()
			.repeatForever()
			.start()
	}

	onClose() {

	}

	onShow() {

	}

	onHide() {

	}

	private onTouchButton(event: Event, customStr) {
		let target: any = event.target;
		if (target.name == 'mask') {
			Framework.layer.close(this);

		} else if (target.name == 'del_btn') {
			MailManager.sendBatchDelMailMsg({ ids: [this.data.id] },()=>{
				Framework.event.fireEvent(GameEvent.MailUpdate);
				Framework.layer.close(this);
			})
		} else if (target.name == 'get_btn') {
			MailManager.sendReadMailMsg({ id: this.data.id },()=>{
				Framework.event.fireEvent(GameEvent.MailUpdate);
				Framework.layer.open(ViewID.MailDetail, null, this.data);
			})
		}

	}

	private updateUI() {
		this.captionTx.string = MailManager.getMailText(this.data.title, true);
		let context = MailManager.getMailText(this.data.content, false)
		this.msgTx.string = `<b>${StringUtil.getLanguageData(context)}</b>`;

		if (this.data.award && this.data.award.size > 0) {
			this.awardsBg.active = true;
			this.awardsSv.numItems = this.data.award.size;
			if (Number(this.data.read) == 1) {
				this.getBtn.active = false;
				this.delBtn.active = true;
				this.getTips.active = false;
			} else {
				this.getBtn.active = true;
				this.delBtn.active = false;
				this.getTips.active = false;
			}
		} else {
			this.awardsBg.active = false;
			this.getBtn.active = false;
			this.delBtn.active = true;
			this.getTips.active = false;
		}
	}

	onEventList(item, idx) {
		item.getComponent(CommonItem).refreshItem(this.data.award[idx]);
	}
}