import Panel from "../views/Panel";
import { MUtils } from "../MUtils";
import PanelCtrl from "../ctrls/panelCtrl";
import { ModuleTypes } from "../types/sceneTypes";
import { MConfigs } from "../MConfigs";
import QuestListItem from "../item/QuestListItem";
import { DataManager } from "../../libs/tw/manager/DataManager";
import { NetName } from "../../libs/tw/enum/NetName";
import { NetBuried } from "../NetBuried";

export default class QuestListPanel extends Panel {
    public get skinKey() { return "QuestListPanel" }
    public get groupName() { return "quest_list" }

    public content:eui.Group;
    public bg:eui.Image;
    public title:eui.Group;
    public separators:eui.Image;
    public list:eui.DataGroup;
    public visit:eui.Group;
    public progressVisit:eui.ProgressBar;
    public others:eui.Group;
    public text3day:eui.Label;
    public text7day:eui.Label;
    public text15day:eui.Label;
    public visit3day:eui.Button;
    public visit7day:eui.Button;
    public visit15day:eui.Button;
    public closeBtn:eui.Button;



    private colorMatrix = [
        0.3,0.6,0,0,0,
        0.3,0.6,0,0,0,
        0.3,0.6,0,0,0,
        0,0,0,1,0
    ];

    private colorFlilter = new egret.ColorMatrixFilter(this.colorMatrix);

    constructor(data?: any) {
        super(data);
    }

    start(data?: any) {
        super.start(data);
    }

    protected async preLoadRes() {
        await RES.loadGroup(this.groupName);
        return new Promise(function (resolve, reject) {
            resolve();
        });
    }

    private showVisitRewardPanel(day: number, reward: number) {
        PanelCtrl.instance.show(ModuleTypes.REWARD_PANEL, {
            title: `完成连续${day}天访问活动`,
            rewardNum: reward
        });
    }

    onSkinComplete() {
        super.onSkinComplete();
        this.updateData()
        NetBuried.ins.showLog(14)
        NetBuried.ins.showLog(15)
        NetBuried.ins.showLog(16)
        NetBuried.ins.showLog(17)
        NetBuried.ins.showLog(18)
        NetBuried.ins.showLog(19)
        NetBuried.ins.showLog(20)
        NetBuried.ins.showLog(21)
    }


    updateData(){

        this.updateLastDays()
       
        let data = [
            {
                id: 0,
                status: 3,
                rewardId: null,
            },
            {
                id: 1,
                status: 0,
                rewardId: null
            },
            {
                id: 2,
                status: 2,
                rewardId: 0
            },
            {
                id: 3,
                status: 0,
                rewardId: null
            },
            {
                id: 4,
                status: 0,
                rewardId: null
            }
        ]
        data.forEach(e => {
            e["context"] = this;
        });

        let dataJson=DataManager.ins.getData(NetName.TASK_INFO_KUWO).data.tasks;
        console.log("dataJson")
        console.log(dataJson)
        
        for(let i=0;i<dataJson.length;i++){

            let index=0
            if(dataJson[i].type==1){
                index=1
            }else if(dataJson[i].type==2){
                index=3
            }else if(dataJson[i].type==3){
                index=4
            }else if(dataJson[i].type==4){
                index=2
            }

            data[index]["type"]=dataJson[i].type
            data[index]["completedCount"]=dataJson[i].completedCount
            data[index]["dailyLimitCount"]=dataJson[i].dailyLimitCount
           
            if(dataJson[i].completedCount>=dataJson[i].dailyLimitCount){
                //已完成
                console.log("已完成")
                data[index].status=3
            }else if(dataJson[i].completedCount<dataJson[i].dailyLimitCount&&dataJson[i].completedCount!=0){
                //进行中
                console.log("进行中")
                data[index].status=1
            }else if(dataJson[i].completedCount==0){
                //去完成
                console.log("去完成")
                data[index].status=0
            }
           
        }

        this.list.dataProvider = new eui.ArrayCollection(data);
        this.list.itemRenderer = QuestListItem;
    }

    updateLastDays(){
        //DataManager.ins.getData(NetName.GET_SIGN_RESULT)
        let continueDay=DataManager.ins.getData(NetName.SIGN_INFO_KUWO).signInfoVO.continueDay;
        this.progressVisit.value=(continueDay-3)/12*100;
        this.visit3day.filters=[this.colorFlilter]
        this.visit7day.filters=[this.colorFlilter]
        this.visit15day.filters=[this.colorFlilter]
        this.text3day.text=`+${window["signPrizes"][1]}金币`
        this.text7day.text=`+${window["signPrizes"][2]}金币`
        this.text15day.text=`+${window["signPrizes"][3]}金币`


        if(continueDay>=3){
            this.visit3day.filters=[]
            this.text3day.text=`+${window["signPrizes"][1]}金币\n(已领取)`
        }
        if(continueDay>=7){
            this.visit7day.filters=[]
            this.text7day.text=`+${window["signPrizes"][2]}金币\n(已领取)`
        }
        if(continueDay>=15){
            this.visit15day.filters=[]
            this.text15day.text=`+${window["signPrizes"][3]}金币\n(已领取)`
        }
        console.log("进度条")
        console.log(continueDay)
        console.log((continueDay-3)/12*100)
    }

    async initEvents() {
        super.initEvents();

       /*  this.visit3day.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            this.showVisitRewardPanel(3, 20);
        }, this);
        this.visit7day.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            this.showVisitRewardPanel(7, 50);
        }, this);
        this.visit15day.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            this.showVisitRewardPanel(15, 100);
        }, this); */

    }
}