/*
 * $URL$
 * $Revision$
 * $LastChangedBy$
 * $LastChangedDate$
 *
 * Copyright (c) 2005 syseca AG, Switzerland
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of syseca AG. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you
 * entered into with syseca AG.
 */
package ch.spherIC.recurvebowsight;

import android.app.Activity;
import android.app.Dialog;

import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import android.text.Html;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import ch.spherIC.recurvebowsight.dialog.ChooseArcherySetupDlg;

import java.util.StringTokenizer;


/**
 * Dialog factory class.
 */
public final class DialogFactory {

    /**
     * Static main method to initialize classes
     *
     * @param   dlgCode  Code of dialog to instantiate. See {@link EasyBracketMain}
     * @param   parent   Parent {@link Activity}
     *
     * @return  Instantiated dialog.
     */
    public static Dialog createDialog(final int dlgCode, final Activity parent) {

        Dialog dlg;

        switch (dlgCode) {

        case RBSMainActivity.DLG_CHOOSE_ARCHERYSETUP:
            dlg = createChooseArcherySetupDialog(parent);
            break;

        case RBSMainActivity.DLG_ABOUT:
            dlg = createAboutDialog(parent);
            break;

        case RBSMainActivity.DLG_USERGUIDE_PARAMS_SIGHT:
            dlg = createUserguideParamsDialog(parent);
            break;

        case RBSMainActivity.DLG_USERGUIDE_RISER_CFG:
            dlg = createUserguideRiserDialog(parent);
            break;

        case RBSMainActivity.DLG_USERGUIDE_SIGHT_CFG:
            dlg = createUserguideSightDialog(parent);
            break;

        case RBSMainActivity.DLG_USERGUIDE_AS_CFG:
            dlg = createUserguideArcherySetupDialog(parent);
            break;

        default:
            dlg = null;
            break;
        }

        return dlg;
    }

    /**
     * @param   parent
     *
     * @return
     */
    private static Dialog createUserguideParamsDialog(final Activity parent) {
        final Dialog dialog = new Dialog(parent);

        dialog.setContentView(R.layout.rbs_userguide_dialog);
        dialog.setTitle(parent.getResources().getText(R.string.menuUserGuide).toString());

        ImageView image = (ImageView) dialog.findViewById(R.id.ug_image);
        image.setImageResource(R.drawable.userguide_params);

        TextView text = (TextView) dialog.findViewById(R.id.ug_txt_HowTo);
        text.setText(parent.getResources().getString(R.string.caption_UserguideParams, parent.getResources().getString(R.string.arrowNockHeightLbl),
                                                     parent.getResources().getString(R.string.targetCenterHeightLbl),
                                                     parent.getResources().getString(R.string.shootingDistanzLbl),
                                                     parent.getResources().getString(R.string.ashNA),
                                                     parent.getResources().getString(R.string.bowPulloutLbl),
                                                     parent.getResources().getString(R.string.asNockRaising),
                                                     parent.getResources().getString(R.string.asBraceHeight),
                                                     parent.getResources().getString(R.string.ug_UserguideParams_Add1,
                                                                                     parent.getResources().getString(R.string.deltaTimeLbl)),
                                                     parent.getResources().getString(R.string.ug_UserguideParams_Add2,
                                                                                     parent.getResources().getString(R.string.calcPrecisionLbl))));

        ((Button) dialog.findViewById(R.id.btn_ugClose)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(final View v) {
                    dialog.dismiss();
                }
            });

        return dialog;
    }

    /**
     * @param   parent
     *
     * @return
     */
    private static Dialog createUserguideRiserDialog(final Activity parent) {
        final Dialog dialog = new Dialog(parent);

        dialog.setContentView(R.layout.rbs_userguide_dialog);
        dialog.setTitle(parent.getResources().getText(R.string.menuUserGuide).toString());

        ImageView image = (ImageView) dialog.findViewById(R.id.ug_image);
        image.setImageResource(R.drawable.userguide_riser);

        TextView text = (TextView) dialog.findViewById(R.id.ug_txt_HowTo);
        String html = String.format(parent.getResources().getText(R.string.caption_UserguideRiserParams).toString(),
                                    parent.getResources().getString(R.string.rcVvisVLbl),                                                                           //
                                    parent.getResources().getString(R.string.rcVvisHLbl),                                                                           //
                                    parent.getResources().getString(R.string.ug_UserguideRiserParams_Add1),
                                    parent.getResources().getString(R.string.ug_UserguideRiserParams_Add2),                                                         //
                                    parent.getResources().getString(R.string.ug_UserguideRiserParams_Add3),
                                    createButtonsGuideString(parent, true)).replaceAll("\n", "<br>");
        text.setText(Html.fromHtml(html));

        ((Button) dialog.findViewById(R.id.btn_ugClose)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(final View v) {
                    dialog.dismiss();
                }
            });

        return dialog;
    }


    /**
     * @param   parent
     *
     * @return
     */
    private static Dialog createUserguideSightDialog(final Activity parent) {
        final Dialog dialog = new Dialog(parent);

        dialog.setContentView(R.layout.rbs_userguide_dialog);
        dialog.setTitle(parent.getResources().getText(R.string.menuUserGuide).toString());

        ImageView image = (ImageView) dialog.findViewById(R.id.ug_image);
        image.setImageResource(R.drawable.userguide_sight);

        TextView text = (TextView) dialog.findViewById(R.id.ug_txt_HowTo);

        StringTokenizer st = new StringTokenizer(parent.getResources().getString(R.string.scaVLbl), "/");
        String aVStr = st.nextToken();
        String aHStr = st.nextToken();
        String html = String.format(parent.getResources().getText(R.string.caption_UserguideSightParams).toString(),                                                         //
                                    aVStr.trim(),
                                    aHStr.trim(),
                                    parent.getResources().getString(R.string.ug_UserguideSightParams_Add1),
                                    parent.getResources().getString(R.string.scHeVSLbl),                                                                                     //
                                    parent.getResources().getString(R.string.ug_UserguideSightParams_Add2),                                                                  //
                                    parent.getResources().getString(R.string.scVertScaleRngMinLbl),
                                    parent.getResources().getString(R.string.scVertScaleRngMaxLbl),
                                    parent.getResources().getString(R.string.scVertSkalaMiddleLbl),
                                    parent.getResources().getString(R.string.scEvSLbl),
                                    createButtonsGuideString(parent, true)).replaceAll("\n", "<br>");
        text.setText(Html.fromHtml(html));

        ((Button) dialog.findViewById(R.id.btn_ugClose)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(final View v) {
                    dialog.dismiss();
                }
            });

        return dialog;
    }

    /**
     * @param   parent
     *
     * @return
     */
    private static Dialog createUserguideArcherySetupDialog(final Activity parent) {
        final Dialog dialog = new Dialog(parent);

        dialog.setContentView(R.layout.rbs_userguide_dialog);
        dialog.setTitle(parent.getResources().getText(R.string.menuUserGuide).toString());

        TextView text = (TextView) dialog.findViewById(R.id.ug_txt_HowTo);

        String str1 = parent.getResources().getString(R.string.ug_UserguideASParams_Add1,
                                                      "<b>" + parent.getResources().getString(R.string.asArcherySetupLbl) + "</b>");
        String str2 = parent.getResources().getString(R.string.ug_UserguideASParams_Add2,
                                                      "<b>" + parent.getResources().getString(R.string.asLabel) + "</b>");
        String str3 = parent.getResources().getString(R.string.ug_UserguideASParams_Add3,
                                                      "<b>" + parent.getResources().getString(R.string.rcRiserLbl) + "</b>");
        String str4 = parent.getResources().getString(R.string.ug_UserguideASParams_Add4,
                                                      "<b>" + parent.getResources().getString(R.string.scSightLbl) + "</b>");
        String str5 = parent.getResources().getString(R.string.ug_UserguideASParams_Add5,
                                                      "<b>" + parent.getResources().getString(R.string.deltaTimeLbl) + "</b>",
                                                      "<b>" + parent.getResources().getString(R.string.calcPrecisionLbl) + "</b>");
        String str6 = parent.getResources().getString(R.string.ug_UserguideASParams_Add6,
                                                      "<b>" + parent.getResources().getString(R.string.scVertSkalaMiddleLbl) + "</b>");
        String str7 = parent.getResources().getString(R.string.ug_UserguideASParams_Add7,
                                                      "<b>" + parent.getResources().getString(R.string.scHeVSLbl) + "</b>");

        String str8 = createButtonsGuideString(parent, true);

        String html = parent.getResources().getString(R.string.caption_UserguideArcherySetupParams, str1, str2, str3,
                                                      str4, str5, str6, str7, str8).replaceAll("\n", "<br>");

        text.setText(Html.fromHtml(html));

        ((Button) dialog.findViewById(R.id.btn_ugClose)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(final View v) {
                    dialog.dismiss();
                }
            });

        return dialog;
    }


    private static Dialog createChooseArcherySetupDialog(final Activity parent) {
        final ChooseArcherySetupDlg dialog = new ChooseArcherySetupDlg(parent);
        return dialog;
    }

    /**
     * Creates the About Dialog
     */
    private static Dialog createAboutDialog(final Activity parent) {

        String versionName = "";
        final Dialog dialog = new Dialog(parent);

        dialog.setContentView(R.layout.rbs_about_dialog);
        dialog.setTitle(parent.getResources().getText(R.string.menuAbout).toString());

        try {
            versionName = parent.getPackageManager().getPackageInfo("ch.spherIC", PackageManager.GET_CONFIGURATIONS).versionName;
        } catch (NameNotFoundException e) {
            versionName = "-";
        }

        ImageView image = (ImageView) dialog.findViewById(R.id.about_imageAppIcon);
        image.setImageResource(R.drawable.ic_launcher);

        TextView text = (TextView) dialog.findViewById(R.id.about_TextDescription);
        text.setText(parent.getResources().getText(R.string.about_Description));

        text = (TextView) dialog.findViewById(R.id.about_TextVersion);
        text.setText(parent.getResources().getText(R.string.AppVersionLabel).toString() + " " + versionName);

        text = (TextView) dialog.findViewById(R.id.about_TextAutor);
        text.setText(parent.getResources().getText(R.string.about_DevelopedBy).toString());

        ((Button) dialog.findViewById(R.id.btn_AboutClose)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(final View v) {
                    dialog.dismiss();
                }
            });

        return dialog;
    }


    private static String createButtonsGuideString(final Activity parent, final boolean useForHtml) {

        String nl = useForHtml ? "<br>" : "\n";
        String bB = useForHtml ? "<b>" : "";
        String bE = useForHtml ? "</b>" : "";

        String btnStr = bB + "Button " + parent.getResources().getString(R.string.caption_New) + ":" + nl + bE
                        + parent.getResources().getString(R.string.caption_Userguide_New) + nl + nl;
        btnStr += bB + "Button " + parent.getResources().getString(R.string.caption_Save) + ":" + nl + bE
                  + parent.getResources().getString(R.string.caption_Userguide_Save) + nl + nl;
        btnStr += bB + "Button " + parent.getResources().getString(R.string.caption_Delete) + ":" + nl + bE
                  + parent.getResources().getString(R.string.caption_Userguide_Delete);

        return btnStr;
    }

}
