import DataModelBase from '../framework/DataModelBase';
import QuestionInfoItem from 'QuestionInfoItem';

// 对局信息，包括玩家信息、房间信息
export default class QuestionInfoModel extends DataModelBase
{
    constructor(modelName)
    {
        super(modelName);

        this.questionInfoItemList = new Array();

        // for (var i = 0; i < 7; ++i)
        // {
        //     this.questionInfoItemList[i] = new QuestionInfoItem()
        // }

        // this.questionInfoItemList[0] = new QuestionInfoItem(0);
        // this.questionInfoItemList[1] = new QuestionInfoItem(1);
        // this.questionInfoItemList[2] = new QuestionInfoItem(2);
        // this.questionInfoItemList[3] = new QuestionInfoItem(3);
        // this.questionInfoItemList[4] = new QuestionInfoItem(4);
        // this.questionInfoItemList[5] = new QuestionInfoItem(5);
        // this.questionInfoItemList[6] = new QuestionInfoItem(6);
        this.current_questionId = 0;
        this.levelData = {};
    }
    
    getQuestionInfo()
    {
        return this.questionInfoItemList;
    }

    setData(pb)
    {
        for (var i = 0; i < pb.questions.length; ++i)
        {
            this.setQuestionInfoByQuestionId(i, pb.questions[i].title, pb.questions[i].answer, pb.questions[i].res_name, pb.questions[i].res_data,pb.questions[i].id,pb.questions[i].three_level_id);
        }
    }

    setQuestionInfoByQuestionId(idx, title, answer, name, data,id,threeId)
    {
        this.questionInfoItemList[idx] = new QuestionInfoItem(idx);
        this.questionInfoItemList[idx].title = title;
        this.questionInfoItemList[idx].id = id;
        this.questionInfoItemList[idx].threeLevelId = threeId
        for (var i = 0; i < answer.length; ++i)
        {
            this.questionInfoItemList[idx].answers[i] = answer[i];
        }
        if (name)
        {
            this.questionInfoItemList[idx].res_name = name;
            this.questionInfoItemList[idx].res_data = data;
            cc.sys.localStorage.setItem(name, data);
            
            if (name.indexOf(".mp3") != -1)
            {
                cc.log('音频题' + idx + '&&&&' + name);
                this.questionInfoItemList[idx].type = 2;
            }
            else if ((name.indexOf(".png") != -1 || name.indexOf(".jpg") != -1))
            {
                cc.log('图片题' + idx + '&&&&' + name)
                this.questionInfoItemList[idx].type = 1;
            }
        }
        else
        {
            cc.log('文字题' + idx + '&&&&' + name)
            this.questionInfoItemList[idx].type = 0;
        }
    }

    getQuestionInfoByQuestionId(idx)
    {
        return this.questionInfoItemList[idx];
    }

    getNumberofQuestion()
    {
        return this.questionInfoItemList.length;
    }

    setLevelData(data)
    {
        this.levelData = data;
    }

    getLevel1DataByLevel(level)
    {
        var oneLevel = this.levelData.one_level;
        if(oneLevel!=null)
        {
            return oneLevel[level];
        }
        return null;
    }

    getLevel2DataByLevel3(level)
    {
        var twoLevel = this.levelData.two_level;
        if(twoLevel!=null)
        {
            return twoLevel[level];
        }
        return null;
    }    

    getLevel3DataById(id)
    {
        var threeLevel = this.levelData.three_level;
        if(threeLevel)
        {
            return threeLevel[id];
        }
        return null;
    }


    getLevel2DataById(id)
    {
       var level3Data = this.getLevel3DataById(id)
       if(level3Data)
       {
            return this.getLevel2DataByLevel3(level3Data.two_level_id);
       }
        return null;

    }


    getLevel1DataById(id)
    {
        var level2Data = this.getLevel2DataById(id)
        if(level2Data)
        {
             return this.getLevel1DataByLevel(level2Data.one_level_id);
        }
         return null; 
    }

}
