package com.example.sleep;


import static android.content.Context.MODE_PRIVATE;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.sleep.database.RecordBean;
import com.example.sleep.database.MyDataBaseHelper;
import com.example.sleep.drawChart.SimpleLineChart;

import java.util.HashMap;

public class SuggestScreen extends Fragment {
    private RecordBean mRecord;//睡眠记录对象

    private SimpleLineChart mSimpleLineChart;//折线图组件
    private TextView suggestion, gradeText;//文本显示组件
    //视图
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chart_fragment, container, false); // 加载布局文件
        init(view);//初始化方法
        return view;//返回视图
    }

    public void init(View view) {
        suggestion = (TextView) view.findViewById(R.id.suggest);//建议文本
        gradeText = (TextView) view.findViewById(R.id.grade);//评分文本
        //调用数据库
        MyDataBaseHelper dbHelp = new MyDataBaseHelper(getActivity());
        SQLiteDatabase sqLiteDatabase = dbHelp.getWritableDatabase();
        try {
            Cursor cursor = sqLiteDatabase.rawQuery("select * from grade_table", null);//从数据库中查询数据

            int counts = Math.min(cursor.getCount(), 7);
            String[] time = new String[counts];//存储时间的数组
            double[] grade = new double[counts];//存储评分的数组
            if (cursor.moveToLast() == true) {
                for (int i = 0; i < counts; i++) {
                    time[i] = cursor.getString(cursor.getColumnIndex("time"));//获取时间并存储到数组中
                    grade[i] = cursor.getDouble(cursor.getColumnIndex("grade"));//获取评分并存储到数组中
                    cursor.moveToPrevious();//移动到上一行
                }
            }
            if (time.length != 0) {
                mSimpleLineChart = (SimpleLineChart) view.findViewById(R.id.simpleLineChart);//获取折线图组件
                String[] yItem = {"100", "80", "60", "40", "20", "0"};
                mSimpleLineChart.setXItem(time);//设置x轴刻度标签
                mSimpleLineChart.setYItem(yItem);//设置y周刻度标签
                HashMap<Integer, Double> pointMap = new HashMap();//存储折线点的hashmap
                for (int i = 0; i < time.length; i++) {
                    pointMap.put(i, grade[i] / 100);//将时间和评分的映射存储到hashmap中
                }
                mSimpleLineChart.setData(pointMap);//设置折线图的数据
                SharedPreferences sharedpref = getActivity().getSharedPreferences("info", MODE_PRIVATE);
                String suggest = sharedpref.getString("suggestion", "");//建议文本
                float gra = sharedpref.getFloat("grade", 0);//评分
                suggestion.setText("睡眠助手的建议：\n"+ suggest);
                long x = Math.round(gra);
                gradeText.setText(x + "");
            } else {
                suggestion.setText("睡眠助手的建议：\n体验一下我们的app吧~");//时间组为空时，显示
            }
        } catch (Exception e) {
            Log.i("e", e.toString());
        }
    }


    private void getSleepTime(){

    }
}