package com.cwbuyer.adapter;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("UseSparseArrays")
public class TableAdapter extends BaseAdapter {  
    private Context context;  
    private List<TableRow> table;  
    
	LinkedList<String> mSizeLink = new LinkedList<String>();
	LinkedList<String> mColorLink = new LinkedList<String>();
	@SuppressLint("UseSparseArrays")
	HashMap<Integer,Integer> mCountRow = new HashMap<Integer,Integer>();
	@SuppressLint("UseSparseArrays")
	HashMap<Integer,String> mCountCell = new HashMap<Integer,String>();
	
	HashMap<Integer,TableRowView> mTableLayout = new HashMap<Integer,TableRowView>(); 
    
    public TableAdapter(Context context, List<TableRow> table) {  
        this.context = context;  
        this.table = table;  
    }  
    @Override  
    public int getCount() {  
        return table.size();  
    }  
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
    public TableRow getItem(int position) {  
        return table.get(position);  
    }  
    
    // 必須要修改此 Function
    public View getView(int position, View convertView, ViewGroup parent) {
    	TableRowView view = null;
        TableRow tableRow = table.get(position);  
        if(mTableLayout.containsKey(position) == true){
        	view = mTableLayout.get(position);
        }else{
        	mTableLayout.put(position, new TableRowView(this.context, tableRow, position));
        	view = mTableLayout.get(position);
        }
        
        for(int i = 0; i < view.getChildCount(); i ++){
        	View child = view.getChildAt(i);
//        	child.setBackgroundResource(R.drawable.keypad_table);
        	child.setOnClickListener(new Clickable(position,i));
        }
        
//        return new TableRowView(this.context, tableRow, position);
        return view;
    }  
    
    public void setData(HashMap<Integer,Integer> row, HashMap<Integer,String> cell){
    	mCountRow = row;
    	mCountCell = cell;
    }
    /** 
     * TableRowView 实现表格行 
     * @author hellogv 
     */  
    public class TableRowView extends LinearLayout {  
        public TableRowView(Context context, TableRow tableRow, int position) {  
            super(context);  
            this.setOrientation(LinearLayout.HORIZONTAL);  
            for (int i = 0; i < tableRow.getSize(); i++) { 
                TableCell tableCell = tableRow.getCellValue(i);  
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
                        tableCell.width, tableCell.height);  
                layoutParams.setMargins(0, 0, 1, 1);  
                if (tableCell.type == TableCell.STRING) {  
                    TextView textCell = new TextView(context);  
                    textCell.setLines(1);  
                    textCell.setGravity(Gravity.LEFT);  
                    textCell.setBackgroundColor(Color.WHITE);
                    if(mCountRow.containsKey(position) == true){
                    	int key = mCountRow.get(position);
                    	if( mCountCell.containsKey(key + i)){
                    		textCell.setText(String.valueOf(mCountCell.get(key + i)));
                    	}else{
                    		textCell.setText("X");
                    	}
                    }else{
                    	textCell.setText("X");
                    }
                    textCell.setTextColor(Color.BLACK);
                    textCell.setTextSize(6);
                    textCell.setOnClickListener(new Clickable(position,i));
                    addView(textCell, layoutParams);  
                } 
                else if (tableCell.type == TableCell.IMAGE) {
                	EditText textCell = new EditText(context);  
                    textCell.setLines(1);  
                    textCell.setGravity(Gravity.LEFT);  
                    textCell.setBackgroundColor(Color.WHITE);
                    if(mCountRow.containsKey(position) == true){
                    	int key = mCountRow.get(position);
                    	if( mCountCell.containsKey(key + i)){
                    		textCell.setText(String.valueOf(mCountCell.get(key + i)));
                    	}else{
                    		textCell.setText("X");
                    	}
                    }else{
                    	textCell.setText("X");
                    }
                    textCell.setTextColor(Color.BLACK);
                    textCell.setTextSize(6);
                    textCell.setOnClickListener(new Clickable(position,i));
                    addView(textCell, layoutParams);  
                }  
            }  
            this.setBackgroundColor(Color.BLACK);
        }  
    }
    
    class Clickable implements OnClickListener{
    	private int mRow;
    	private int mCell;
    	Clickable(int row, int cell){
    		mRow = row;
    		mCell = cell;
    	}
		@Override
		public void onClick(View v) {
			Toast.makeText(context, "Click " + mRow + " , " + mCell, Toast.LENGTH_SHORT).show();
		}
    }
    
    /** 
     * TableRow 实现表格?��? 
     * @author hellogv 
     */  
    static public class TableRow {  
        private TableCell[] cell;  
        public TableRow(TableCell[] cell) {  
            this.cell = cell;  
        }  
        public int getSize() {  
            return cell.length;  
        }  
        public TableCell getCellValue(int index) {  
            if (index >= cell.length)  
                return null;  
            return cell[index];  
        }  
    }  
    /** 
     * TableCell 实现表格?�格?��? 
     * @author hellogv 
     */  
    static public class TableCell {  
        static public final int STRING = 0;  
        static public final int IMAGE = 1;  
//        public Object value;  
        public int width;  
        public int height;  
        private int type;  
//        public TableCell(Object value, int width, int height, int type) {
        public TableCell(int width, int height, int type) {
//            this.value = value;  
            this.width = width;  
            this.height = height;  
            this.type = type;  
        }  
    }
}
