package com.example.sleep;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.sleep.database.GetRecord;
import com.example.sleep.database.RecordBean;
import com.example.sleep.recyclerView.Trace;
import com.example.sleep.recyclerView.TraceListAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.example.sleep.database.GetRecord.getRecord;

/**
 * 睡眠记录页面
 */
public class Record extends Activity {

    private RecyclerView rvTrace;
    private List<Trace> traceList = new ArrayList<>();//睡眠记录列表

    private String date = "";//日期

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.records);
        rvTrace = findViewById(R.id.timelList);
        date = this.getIntent().getStringExtra("date");
        initData();
    }

    //睡眠记录数据初始化
    private void initData() {
        GetRecord mGetRecord = getRecord();//获取数据库操作对象
        //List<RecordBean> records = mGetRecord.queryAllList();
        List<RecordBean> records = mGetRecord.queryByDate(date);// 根据日期查询睡眠记录
        if (records.size() == 1)
        {
            //如果只有一条记录，直接跳转到记录详情页面
            Intent i = new Intent(Record.this, SleepresultScreen.class);
            i.putExtra("date", date);
            i.putExtra("position", 0);
            Record.this.startActivity(i);
            Record.this.finish();
        } else {
            //如果有多条记录，将记录添加到列表中，并设置 RecyclerView 的适配器
            for (RecordBean e : records) {
                traceList.add(new Trace(e.getDate(), e.getStartTime() + "-" + e.getEndTime()
                        + "  " + e.getTotalTime() / 60 + "时" + e.getTotalTime() % 60 + "分"));
            }
            TraceListAdapter adapter = new TraceListAdapter(this, traceList, date);
            rvTrace.setLayoutManager(new LinearLayoutManager(this));
            rvTrace.setAdapter(adapter);
        }
    }

    //左上的返回
    public void ClickBack(View v) {
        Record.this.finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}