package com.hyperling.carbupbeta;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TabHost;

import java.util.ArrayList;

/**
 * Created by usb on 9/29/14.
 */
public class Main extends TabActivity {

    private DALIH dalih;

    ArrayList<ListInfo> lists;

    boolean createTab2 = false;
    int lastList, openTab, textSize;
    String tab2Name;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        // Instantiate DALIH
        dalih = new DALIH(this);

        dalih.open();
        // THIS IS AN ASSIGNMENT STATEMENT. PLEASE READ IT CORRECTLY. #savecycles
        if ((lastList = dalih.getLastList()) >= 0) {
            openTab = dalih.getOpenTab();
            lists = dalih.getLists();
        }
        dalih.close();

        if (lastList >= 0){
            createTab2 = true;

            for (ListInfo list : lists){
                if (list.getID() == lastList){
                    tab2Name = list.getName();
                }
            }
        }

        // Init the TabHost
        TabHost tabHost = getTabHost();

        // Create the tabs
        // Tab1
        TabHost.TabSpec createListSpec = tabHost.newTabSpec("Create List");
        if (Build.VERSION.SDK_INT >= 21) {
            createListSpec.setIndicator("Choose List", getDrawable(R.drawable.dr_face_straight2_circle));
        }
        else{
            createListSpec.setIndicator("Choose List");
        }
        Intent createList = new Intent(this, Tab1.class);
        createListSpec.setContent(createList);
        // Add tab to TabHost
        tabHost.addTab(createListSpec);

        // Tab2
        if (createTab2) {
            if (dalih.DEBUG) System.out.println("Creating tab2");

            TabHost.TabSpec createItemsSpec = tabHost.newTabSpec("Create Items");
            if (Build.VERSION.SDK_INT >= 21) {
                createItemsSpec.setIndicator(tab2Name);
            }
            else{
                createItemsSpec.setIndicator(tab2Name);
            }
            Intent createItems = new Intent(this, Tab2.class);
            createItemsSpec.setContent(createItems);

            // Add tab to TabHost
            tabHost.addTab(createItemsSpec);
        }

        // Settings/About Tab
        TabHost.TabSpec mySettingsSpec = tabHost.newTabSpec("Settings");
        if (Build.VERSION.SDK_INT >= 21) {
            mySettingsSpec.setIndicator("Settings", getDrawable(R.drawable.figsy_grass_circle_small));
        }
        else{
            mySettingsSpec.setIndicator("Settings");
        }
        Intent mySettingsList = new Intent(this, MySettings.class);
        mySettingsSpec.setContent(mySettingsList);
        // Add tab to TabHost
        tabHost.addTab(mySettingsSpec);


        // BEFORE FINAL, APP SHOULD OPEN ON TAB2
        //tabHost.setCurrentTab(1);
        // After app is final, app should open to tab1 if no previous lists exist
        // Otherwise, the app will open on tab 2 with the last selected list in place
        if (dalih.DEBUG) System.out.println("Opening tab " + openTab + ", index " + (openTab-1));
        tabHost.setCurrentTab(openTab-1);
        return;
    }
}
