import {Component, ViewChild} from '@angular/core';
import {IonicPage, Navbar, NavController, NavParams, Slides, ToastController} from 'ionic-angular';
import {message} from "../../../../app/main";
import {AppGlobal, AppService} from "../../../../service/appHttpService";
import {LearningResultPage} from "../learning-result/learning-result";
import {LearnService} from "../learn.service";
import {DatePipe} from "@angular/common";

@IonicPage()
@Component({
    selector: 'page-learning-do',
    templateUrl: 'learning-do.html',
})
export class LearningDoPage {
    @ViewChild(Slides) slides: Slides;
    @ViewChild(Navbar) navbar: Navbar

    clock;  //倒计时的定时器
    list = [];
    timeText = '00:00:00';
    totalTime;
    index = 1;  //当前题目的序号
    constructor(public navCtrl: NavController, public navParams: NavParams, private learnSer: LearnService,
                public toastCtrl: ToastController, public appService: AppService, public datePipe: DatePipe) {
    }

    ionViewDidEnter() {
        const testId = this.navParams.get('testId');
        const data = {
            testId: testId
        };
        this.learnSer.startTest(data).subscribe(
            (res) => {
                this.list = res.data.detailVos;
                this.list.forEach(e=>e.answerdesc = '');
                this.totalTime = res.data.timeLimit;
                this.countTime();
            }
        )
        //返回按钮的提示
        this.navbar.backButtonClick = (event) => {
            let index = this.navCtrl.length() - 2;
            this.navCtrl.remove(2, index)
        }
    }

    //清楚定时器
    ionViewDidLeave(){
        this.index = 1;
        this.slides.slideTo(0);
        window.clearInterval(this.clock);
    }

    //倒计时
    countTime() {
        let totalTime = this.totalTime;
        this.clock = window.setInterval(() => {
            totalTime--;
            let hourse = (Math.floor(totalTime / 3600)).toString();
            hourse = (hourse.length > 1 ? hourse:'0'+ hourse);
            let minutes = Math.floor(totalTime / 60).toString();
            minutes = (minutes.length  > 1 ? minutes: '0'+ minutes);
            let seconds = Math.floor(totalTime % 60).toString();
            seconds = (seconds.length > 1 ? seconds: '0'+ seconds );
            this.timeText = hourse + ":" + minutes + ":" + seconds;
            if (totalTime < 0) {
                window.clearInterval(this.clock)
                this.timeText = "00:00:00";
                this.forceSubmit();
            }
        }, 1000)
    }

    //上一题
    prev() {
        if (this.index == 1) {
            const toast = this.toastCtrl.create(message);
            toast.setMessage('已经是第一题哦');
            toast.present();
        } else {
            this.index -= 1;
            this.slides.slideTo(this.index - 1)
        }
    }

    //下一题
    next() {
        if (this.index == this.list.length) {
            const toast = this.toastCtrl.create(message);
            toast.setMessage('已经是最后一题了哦');
            toast.present();
        } else {
            this.index += 1;
            this.slides.slideTo(this.index - 1)
        }
    }

    //多选
    mutiSelect(i, option) {
        if(this.list[i].answerdesc.includes(option)){
            this.list[i].answerdesc = this.list[i].answerdesc.replace(option,'');
        }else{
            this.list[i].answerdesc += option;
        }
        console.log(this.list);
    }

    //强制交卷
    forceSubmit(){
        this.appService.alert("答题时间已到，交卷！");
        let index = this.navCtrl.length() - 2;
        this.navCtrl.remove(2, index)
    }

    //交卷
    submit() {
        let message = "确定交卷吗";   //提示信息
        let number = 0;  //有number题未答
        this.list.forEach(e=>{
            if(e.answerdesc.length  == 0){
                number ++;
            }
        });

        if(number > 0){
            message = "您有"+number+"题未作答，确定交卷吗";
        }
        console.log(this.list);
        this.appService.alert(message, () => {
            this.navCtrl.push("LearningResultPage")
        })
    }

}
