package me.yoqi.android.jkbd.dao;

import android.content.Intent;
import android.util.Log;

import java.util.List;

import me.yoqi.android.jkbd.MyApp;
import me.yoqi.android.jkbd.bean.Exam;
import me.yoqi.android.jkbd.bean.Question;
import me.yoqi.android.jkbd.bean.Result;
import me.yoqi.android.jkbd.utils.OkHttpUtils;
import me.yoqi.android.jkbd.utils.ResultUtils;


/**
 * Created by LEO on 2017/6/30.
 */

public class ExamDao implements IExamDao{
    @Override
    public void loadExamInfo() {
        OkHttpUtils<Exam> utils=new OkHttpUtils<>(MyApp.getInstance());
        String uri="http://101.251.196.90:8080/JztkServer/examInfo";
        utils.url(uri)
                .targetClass(Exam.class)
                .execute(new OkHttpUtils.OnCompleteListener<Exam>(){

                    @Override
                    public void onSuccess(Exam exam) {

                        Log.e("main","result=------"+exam);

                        MyApp.getInstance().setExam(exam);

                        MyApp.getInstance()
                                .sendBroadcast(new Intent(MyApp.LOAD_EXAM)
                                                    .putExtra(MyApp.LOAD_DATA_SUCCESS,true));
                    }

                    @Override
                    public void onError(String error) {
                        Log.e("main","error="+error);
                        MyApp.getInstance()
                                .sendBroadcast(new Intent(MyApp.LOAD_EXAM)
                                        .putExtra(MyApp.LOAD_DATA_SUCCESS,false));
                    }
                });
    }

    @Override
    public void loadQuestionLists() {
        OkHttpUtils<String> utils=new OkHttpUtils<>(MyApp.getInstance());
        String uri="http://101.251.196.90:8080/JztkServer/getQuestions?testType=rand";
        utils.url(uri)
                .targetClass(String.class)
                .execute(new OkHttpUtils.OnCompleteListener<String>(){
                    @Override
                    public void onSuccess(String jsonStr) {
                        boolean success=false;
                        Result result= ResultUtils.getListResultFromJson(jsonStr);
                        if(result!=null&&result.getError_code()==0){
                            List<Question> list=result.getResult();
                            if(list!=null&&list.size()>0){
//                                mExamList=list;
                                MyApp.getInstance().setQuestionList(list);
                                success=true;
                            }
                        }
                        MyApp.getInstance()
                                .sendBroadcast(new Intent(MyApp.LOAD_QUESTION)
                                        .putExtra(MyApp.LOAD_DATA_SUCCESS,success));
                    }

                    @Override
                    public void onError(String error) {
                        Log.e("main","error="+error);

                        MyApp.getInstance()
                                .sendBroadcast(new Intent(MyApp.LOAD_QUESTION)
                                        .putExtra(MyApp.LOAD_DATA_SUCCESS,false));
                    }
                });
    }
}
