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) {
    }

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

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

    //清楚定时器
    ionViewDidLeave(){
        window.clearInterval(this.clock);
    }

    //上一题
    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, quesIndex) {

    }

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

    //交卷
    submit() {
        this.appService.alert("确定提交问卷", () => {
            this.navCtrl.push("LearningResultPage")
        })
    }

}
