package com.example.yochi.shiftcal;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.Date;
import java.sql.Timestamp;

/**
 * Created by yochi on 2017/01/17.
 */
class DateAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private SQLiteDatabase mydb;
    private Timestamp top = null;

    private static class DateViewHolder {
        public TextView  title;
        public TextView  contents;
    }

    DateAdapter(Context context) {
      mContext = context;
      mLayoutInflater = LayoutInflater.from(context);

      if(top == null){
        // 現在月の１日の曜日からカレンダー表示の左上日付を求める
        top = new Timestamp(new Date().getTime());

      }
      // カレンダー最終日（top+42日）
      Timestamp bottom = new Timestamp(top.getTime());

      // calテーブルからカレンダー表示分のデータ取得
      MySQLiteOpenHelper hlpr = new MySQLiteOpenHelper(mContext);
      mydb = hlpr.getWritableDatabase();
      String format = "select date, shift from cal where date bitween %s and %s";
      String sql = String.format(format, top, bottom);
      Cursor c = mydb.rawQuery(sql);
      f(c.moveToFirst()){
        do{
          long date = c.getLong(c.getColumnIndex("date"));
          long shift = c.getLong(c.getColumnIndex("shift"));
        }while(c.moveToNext());
      }
    }

    public int getCount() {
        return 7*6;
    }

    public Object getItem(int position) {
        // 日にち計算
        return position + "日";
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

      DateViewHolder holder;
      if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.date_layout, null);
        holder = new DateViewHolder();
        convertView.setTag(holder);
      } else {
          holder = (DateViewHolder)convertView.getTag();
      }

      holder.title.setText((String)getItem(position));

      return convertView;
    }
}

@@ -557,41 +557,8 @@
 er;%0A
-    private SQLiteDatabase mydb;%0A
     
@@ -587,16 +587,16 @@
 = null;%0A
+
 %0A    pri
@@ -1068,480 +1068,38 @@
     
-MySQLiteOpenHelper hlpr = new MySQLiteOpenHelper(mContext);%0A      mydb = hlpr.getWritableDatabase();%0A      String format = %22select date, shift from cal where date bitween %25s and %25s%22;%0A      String sql = String.format(format, top, bottom);%0A      Cursor c = mydb.rawQuery(sql);%0A      f(c.moveToFirst())%7B%0A        do%7B%0A          long date = c.getLong(c.getColumnIndex(%22date%22));%0A          long shift = c.getLong(c.getColumnIndex(%22shift%22));%0A        %7Dwhile(c.moveToNext());%0A      %7D
+ShiftDao dao = new ShiftDao();
 %0A   

add dao class