package com.cwbuyer.adapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cwbuyer.lib.AppUtil;
import com.cwbuyer.lib.DateUtil;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

@SuppressLint("InflateParams")
@SuppressWarnings("unused")
public class DateAdapter extends BaseAdapter{
	private LayoutInflater mInflater;
	private int[] mImgs = new int[]{R.drawable.img_date1,R.drawable.img_date2,R.drawable.img_date3};
	private String[] mNames = null;
	private int nPart=0;
	private String mKind=" and QKIND='30'";
    
    private String[] mDates = new String[3];
    private String[] mScores = new String[3];
	private String[] strDays= new String[3];
    
    private List<Integer> subActionId = new ArrayList<Integer>();
	private List<Integer> subScore = new ArrayList<Integer>();
    

    /* MyAdapter篶肚把计 */
    public DateAdapter(Context context) {
      /* 把计﹍て */
      mInflater = LayoutInflater.from(context);
      mNames = new String[]{context.getString(R.string.str_day_report),
    		  context.getString(R.string.str_week_report),
    		  context.getString(R.string.str_month_report)};
     try {
		mKind=" and (QKIND='31' or QKIND='30')";
   		refresh(context);
    	
     } catch (Exception e) {
         Log.i("DB", "DB_DIR_Exception: " + e.toString());
       }
    }
    
    public void refresh(Context context){
	    	SQLiteDatabase db = Utilis.getDB(context);
			Cursor cursor;
			StringBuffer queryDay = new StringBuffer();
			int nScores[]=new int[2];
	    	
			  
	    	String FormD=DateUtil.getCurrentDate();  //2012-10-22
			int Ttime=Utilis.toInt(DateUtil.getSystemTime().substring(8, 10));  //眔ヘ玡碭翴
			if (Ttime<=5)  FormD = DateUtil.addDate(FormD, -1);
			
			 mDates[0] = FormD+"~"+FormD;
			FormD = FormD.replace("/", "-");
			strDays[0]=FormD;strDays[1]=FormD;
			nScores[0]=0;nScores[1]=0;
			queryDay.append("Select sum(ATOT) From qhead where ISCONFIRM='Y' ").append(mKind)
			.append(" and FORMDATE ='" + FormD + "'");
    		cursor = db.rawQuery(queryDay.toString(), null);
    		if (cursor.getCount()>0) {
    			cursor.moveToFirst();
    			nScores[0]=cursor.getInt(0);  //参璸ン计
    		}
    		cursor.close();
    		
    		queryDay.delete(0, queryDay.toString().length());
			queryDay.append("Select sum(CASH),sum(CARD),sum(ATM) From qhead where ISCONFIRM='Y'").append(mKind)
			.append(" and FINEDATE ='" + FormD + "'");
    		cursor = db.rawQuery(queryDay.toString(), null);
    		if (cursor.getCount()>0) {
    			cursor.moveToFirst();
    			nScores[1]=cursor.getInt(0)+cursor.getInt(1)+cursor.getInt(2);
    		}
    		cursor.close();
			
    		queryDay.delete(0, queryDay.toString().length());
			queryDay.append("Select sum(ORDCASH),sum(ORDCARD) From qhead where ISCHECK='Y'").append(mKind)
			.append(" and ORDDATE ='" +FormD+ "'");
    		cursor = db.rawQuery(queryDay.toString(), null);
    		if (cursor.getCount()>0) {
    			cursor.moveToFirst();
    			nScores[1]=nScores[1]+cursor.getInt(0)+cursor.getInt(1);
    		}
    		cursor.close();
    		mScores[0] = "T:"+nScores[0]+" $:" + nScores[1];	    			

		//盞絏璶镑,秅,る
		
        mDates[1] = getWeekDate();
		strDays=mDates[1].split("~");

		if(strDays.length >= 2){
			nScores[0]=0;nScores[1]=0;
			queryDay.delete(0, queryDay.toString().length());
			queryDay.append("Select sum(ATOT) From qhead where ISCONFIRM='Y'").append(mKind)
			.append(" and FORMDATE >='" + DateUtil.toDBDate(strDays[0]) + "' and FORMDATE <= '" + DateUtil.toDBDate(strDays[1]) + "'");
    		cursor = db.rawQuery(queryDay.toString(), null);
    		if (cursor.getCount()>0) {
    			cursor.moveToFirst();
    			nScores[0]=cursor.getInt(0);  //参璸ン计
    		}
    		cursor.close();
    		
    		queryDay.delete(0, queryDay.toString().length());
			queryDay.append("Select sum(CASH),sum(CARD),sum(ATM) From qhead where ISCONFIRM='Y'").append(mKind)
			.append(" and (FINEDATE >='" + DateUtil.toDBDate(strDays[0]) + "' and FINEDATE <= '" + DateUtil.toDBDate(strDays[1]) + "')");
    		cursor = db.rawQuery(queryDay.toString(), null);
    		if (cursor.getCount()>0) {
    			cursor.moveToFirst();
    			nScores[1]=cursor.getInt(0)+cursor.getInt(1)+cursor.getInt(2);
    		}
    		cursor.close();
			
    		queryDay.delete(0, queryDay.toString().length());
			queryDay.append("Select sum(ORDCASH),sum(ORDCARD) From qhead where ISCHECK='Y'").append(mKind)
			.append(" and (ORDDATE >='" + DateUtil.toDBDate(strDays[0]) + "' and ORDDATE <= '" + DateUtil.toDBDate(strDays[1]) + "')");
    		cursor = db.rawQuery(queryDay.toString(), null);
    		if (cursor.getCount()>0) {
    			cursor.moveToFirst();
    			nScores[1]=nScores[1]+cursor.getInt(0)+cursor.getInt(1);
    		}
    		cursor.close();
    		mScores[1] = "T:"+nScores[0]+" $:" + nScores[1];	    			
		}
        
        mDates[2] = getMonthDate();
		strDays=mDates[2].split("~");

		if(strDays.length >= 2){
			nScores[0]=0;nScores[1]=0;
			queryDay.delete(0, queryDay.toString().length());
			queryDay.append("Select sum(ATOT) From qhead where ISCONFIRM='Y'").append(mKind)
			.append(" and FORMDATE >='" + DateUtil.toDBDate(strDays[0]) + "' and FORMDATE <= '" + DateUtil.toDBDate(strDays[1]) + "'");
    		cursor = db.rawQuery(queryDay.toString(), null);
    		if (cursor.getCount()>0) {
    			cursor.moveToFirst();
    			nScores[0]=cursor.getInt(0);  //参璸ン计
    		}
    		cursor.close();
    		
    		queryDay.delete(0, queryDay.toString().length());
			queryDay.append("Select sum(CASH),sum(CARD),sum(ATM) From qhead where ISCONFIRM='Y'").append(mKind)
			.append(" and (FINEDATE >='" + DateUtil.toDBDate(strDays[0]) + "' and FINEDATE <= '" + DateUtil.toDBDate(strDays[1]) + "')");
    		cursor = db.rawQuery(queryDay.toString(), null);
    		if (cursor.getCount()>0) {
    			cursor.moveToFirst();
    			nScores[1]=cursor.getInt(0)+cursor.getInt(1)+cursor.getInt(2);
    		}
    		cursor.close();
			
    		queryDay.delete(0, queryDay.toString().length());
			queryDay.append("Select sum(ORDCASH),sum(ORDCARD) From qhead where ISCHECK='Y'").append(mKind)
			.append(" and (ORDDATE >='" + DateUtil.toDBDate(strDays[0]) + "' and ORDDATE <= '" + DateUtil.toDBDate(strDays[1]) + "')");
    		cursor = db.rawQuery(queryDay.toString(), null);
    		if (cursor.getCount()>0) {
    			cursor.moveToFirst();
    			nScores[1]=nScores[1]+cursor.getInt(0)+cursor.getInt(1);
    		}
    		cursor.close();
    		mScores[2] = "T:"+nScores[0]+" $:" + nScores[1];
		}
		db.close();
        // 穓碝┮Τら戳だ计!
    	this.notifyDataSetChanged();
	
    }
    
    /* 膥┯BaseAdapter惠滦糶method */
    public int getCount() {
      return 3;
    }

    public Object getItem(int position) {
      return position;
    }

    public long getItemId(int position) {
      return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder;
      if (convertView == null) {
    	convertView = mInflater.inflate(R.layout.list_date_index, null);  
        holder = new ViewHolder();
        holder.image1 = (ImageView)convertView.findViewById(R.id.icon_date);
        holder.textName = (TextView)convertView.findViewById(R.id.tv_name);
        holder.textDate = (TextView)convertView.findViewById(R.id.tv_date);
        holder.textScore = (TextView)convertView.findViewById(R.id.tv_score);
        
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }
      holder.image1.setImageResource(mImgs[position]);
      holder.textName.setText(mNames[position]);
      holder.textDate.setText(mDates[position]);
      holder.textScore.setText(mScores[position]);
      
      return convertView;
    }

    /* class ViewHolder */
    private class ViewHolder {
      ImageView image1;
      TextView textName;
      TextView textDate;
      TextView textScore;
    }

    public void removeitem(int index) {
      if (index < 0)
        return;
      notifyDataSetChanged();
    }
    
    private String getMonthDate(){
    	StringBuffer buf = new StringBuffer();
    	int nYear1 = 0;
    	int nMonth1 = 0;
    	int nDay1 = 0;
    	int nYear2 = 0;
    	int nMonth2 = 0;
    	int nDay2 = 0;
    	Calendar c = Calendar.getInstance();
    	
    	c.set(Calendar.DAY_OF_MONTH, 1); // 闽龄
    	
    	nYear1 = c.get(Calendar.YEAR);
		nMonth1 = c.get(Calendar.MONTH) + 1;
		nDay1 = c.get(Calendar.DAY_OF_MONTH);
		
		c.add(Calendar.MONTH, 1);
		c.add(Calendar.DATE, -1);   // 闽龄
		
		nYear2 = c.get(Calendar.YEAR);
		nMonth2 = c.get(Calendar.MONTH) + 1;
		nDay2 = c.get(Calendar.DAY_OF_MONTH);
		
		buf.append(nYear1).append("/");
    	buf.append(Utilis.formatDate(nMonth1)).append("/");
    	buf.append(Utilis.formatDate(nDay1)).append("~");
    	
    	buf.append(nYear2).append("/");
    	buf.append(Utilis.formatDate(nMonth2)).append("/");
    	buf.append(Utilis.formatDate(nDay2));
    	
    	return buf.toString();
    }
    
    
    private String getWeekDate(){
    	StringBuffer buf = new StringBuffer();
    	int nYear1 = 0;
    	int nMonth1 = 0;
    	int nDay1 = 0;
    	int nYear2 = 0;
    	int nMonth2 = 0;
    	int nDay2 = 0;
    	
    	Calendar c = Calendar.getInstance();
//    	c.add(Calendar.DAY_OF_MONTH, 1);
    	int day = c.get(Calendar.DAY_OF_WEEK);
    	if(day == Calendar.SUNDAY){
    		c.add(Calendar.DAY_OF_MONTH, -1);
    	}
    	
    	c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    	c.setTime(c.getTime());
    	
    	nYear1 = c.get(Calendar.YEAR);
    	nMonth1 = c.get(Calendar.MONTH) + 1;
    	nDay1 = c.get(Calendar.DAY_OF_MONTH);
    	
    	buf.append(nYear1).append("/");
    	buf.append(Utilis.formatDate(nMonth1)).append("/");
    	buf.append(Utilis.formatDate(nDay1)).append("~");
    	
    	c.add(Calendar.DAY_OF_MONTH, 6);
    	
    	nYear2 = c.get(Calendar.YEAR);
    	nMonth2 = c.get(Calendar.MONTH) + 1;
    	nDay2 = c.get(Calendar.DAY_OF_MONTH);
    	
    	buf.append(nYear2).append("/");
    	buf.append(Utilis.formatDate(nMonth2)).append("/");
    	buf.append(Utilis.formatDate(nDay2));
    	
    	return buf.toString();
    }
    
    private String getDayDate(){
    	StringBuffer buf = new StringBuffer();
    	int nYear1 = 0;
    	int nMonth1 = 0;
    	int nDay1 = 0;
    	Calendar c = Calendar.getInstance();
    	nYear1 = c.get(Calendar.YEAR);
    	nMonth1 = c.get(Calendar.MONTH) + 1;
    	nDay1 = c.get(Calendar.DAY_OF_MONTH);
    	
    	buf.append(nYear1).append("/");
    	buf.append(Utilis.formatDate(nMonth1)).append("/");
    	buf.append(Utilis.formatDate(nDay1)).append("~");
    	buf.append(nYear1).append("/");
    	buf.append(Utilis.formatDate(nMonth1)).append("/");
    	buf.append(Utilis.formatDate(nDay1));
    	
    	
    	return buf.toString();
    }
    
}
