/*
 * Copyright (C) 2013 Brian Muramatsu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.app;

import java.util.Scanner;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.btmura.android.reddit.R;
import com.btmura.android.reddit.content.ThemePrefs;

public class MarkdownTableFragment extends DialogFragment {

    static final String TAG = "MarkdownTableFragment";

    private static final String EXTRA_TABLE_DATA = "tableData";

    public interface OnMarkdownTableFragmentEventListener {
        void onCancel();
    }

    private OnMarkdownTableFragmentEventListener listener;

    public static MarkdownTableFragment newInstance(String tableData) {
        Bundle args = new Bundle(1);
        args.putString(EXTRA_TABLE_DATA, tableData);
        MarkdownTableFragment frag = new MarkdownTableFragment();
        frag.setArguments(args);
        return frag;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof OnMarkdownTableFragmentEventListener) {
            listener = (OnMarkdownTableFragmentEventListener) activity;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, ThemePrefs.getDialogTheme(getActivity()));
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.markdown_table, container, false);
        TableLayout tableLayout = (TableLayout) view.findViewById(R.id.table);

        String tableData = getTableDataExtra();
        Scanner scanner = new Scanner(tableData);
        for (int row = 0; scanner.hasNextLine(); row++) {
            String[] cells = scanner.nextLine().split("\\|");
            int cellCount = cells.length;
            TableRow tableRow = new TableRow(getActivity());
            for (int j = 0; j < cellCount; j++) {
                int layout = row == 0
                        ? R.layout.markdown_table_cell_header
                        : R.layout.markdown_table_cell;
                TextView tv = (TextView) inflater.inflate(layout, container, false);
                tv.setText(cells[j]);
                tableRow.addView(tv);
            }
            tableLayout.addView(tableRow);
        }
        scanner.close();

        return view;
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (listener != null) {
            listener.onCancel();
        }
        super.onCancel(dialog);
    }

    private String getTableDataExtra() {
        return getArguments().getString(EXTRA_TABLE_DATA);
    }
}

@@ -806,16 +806,45 @@
 agment;%0A
+import android.view.Gravity;%0A
 import a
@@ -2599,32 +2599,67 @@
 ner(tableData);%0A
+        int%5B%5D gravitySpecs = null;%0A
         for (int
@@ -2911,24 +2911,144 @@
 unt; j++) %7B%0A
+                if (row == 1) %7B%0A                    gravitySpecs = getGravitySpecs(cells);%0A                %7D else %7B%0A    
             
@@ -3073,16 +3073,20 @@
 ow == 0%0A
+    
         
@@ -3163,16 +3163,20 @@
         
+    
 : R.layo
@@ -3195,24 +3195,200 @@
 table_cell;%0A
+                    int gravity = gravitySpecs != null && j %3C gravitySpecs.length%0A                            ? gravitySpecs%5Bj%5D%0A                            : Gravity.LEFT;%0A    
             
@@ -3476,16 +3476,20 @@
         
+    
 tv.setTe
@@ -3502,16 +3502,64 @@
 ls%5Bj%5D);%0A
+                    tv.setGravity(gravity);%0A    
         
@@ -3588,16 +3588,34 @@
 ew(tv);%0A
+                %7D%0A
         
@@ -3719,32 +3719,732 @@
 rn view;%0A    %7D%0A%0A
+    // TODO(btmura): look up specification of how many dashes and colons are required%0A    private int%5B%5D getGravitySpecs(String%5B%5D cells) %7B%0A        int cellCount = cells.length;%0A        int%5B%5D specs = new int%5BcellCount%5D;%0A        for (int i = 0; i %3C cellCount; i++) %7B%0A            if (cells%5Bi%5D.length() %3E 2 && cells%5Bi%5D.startsWith(%22:%22) && cells%5Bi%5D.endsWith(%22:%22)) %7B%0A                specs%5Bi%5D = Gravity.CENTER;%0A            %7D else if (cells%5Bi%5D.startsWith(%22:%22)) %7B%0A                specs%5Bi%5D = Gravity.LEFT;%0A            %7D else if (cells%5Bi%5D.endsWith(%22:%22)) %7B%0A                specs%5Bi%5D = Gravity.RIGHT;%0A            %7D else %7B%0A                specs%5Bi%5D = Gravity.LEFT;%0A            %7D%0A        %7D%0A        return specs;%0A    %7D%0A%0A
     @Override%0A  

Add untested alignment code for tables