/*
 * $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.resultlist;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import ch.spherIC.R;


/**
 * Exposure adapter view class.
 */
class ExposureAdapterView extends LinearLayout {


    /**
     * Creates a new ExposureAdapterView object.
     *
     * @param  context  DOCUMENT ME!
     * @param  weather  DOCUMENT ME!
     */
    public ExposureAdapterView(Context context, Exposure exposure) {
        super(context);
        this.setOrientation(HORIZONTAL);

        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int width = metrics.widthPixels;

        // Nbr
        TextView nbrControl = new TextView(context);
        LinearLayout.LayoutParams nbrParams = new LinearLayout.LayoutParams(Math.round(0.14583f * width),
                                                                            LayoutParams.WRAP_CONTENT, 0.2f);
        nbrParams.setMargins(3, 1, 1, 1);
        nbrControl.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        nbrControl.setTextAppearance(context, R.style.ResultExposureText);
        nbrControl.setText(Integer.toString(exposure.getNbr()));
        addView(nbrControl, nbrParams);

        // Aperture
        TextView apertureControl = new TextView(context);
        LinearLayout.LayoutParams apertureParams = new LinearLayout.LayoutParams(Math.round(0.20833f * width),
                                                                                 LayoutParams.WRAP_CONTENT, 0.2f);
        apertureParams.setMargins(1, 1, 1, 1);
        apertureControl.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        apertureControl.setTextAppearance(context, R.style.ResultExposureText);
        apertureControl.setText(exposure.getAperture());
        addView(apertureControl, apertureParams);

        // Shutter speed
        TextView shutterControl = new TextView(context);
        LinearLayout.LayoutParams shutterParams = new LinearLayout.LayoutParams(Math.round(0.35417f * width),
                                                                                LayoutParams.WRAP_CONTENT, 0.2f);
        shutterParams.setMargins(1, 1, 1, 1);
        shutterControl.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        shutterControl.setTextAppearance(context, R.style.ResultExposureTextSpecial);
        shutterControl.setText(exposure.getShutterSpeed());
        addView(shutterControl, shutterParams);

        // EV Step
        TextView evStepControl = new TextView(context);
        LinearLayout.LayoutParams evStepParams = new LinearLayout.LayoutParams(Math.round(0.14583f * width),
                                                                               LayoutParams.WRAP_CONTENT, 0.2f);
        evStepParams.setMargins(1, 1, 1, 1);
        evStepControl.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        evStepControl.setTextAppearance(context, R.style.ResultExposureText);
        evStepControl.setText(exposure.getEvStepAsString());
        addView(evStepControl, evStepParams);

        // Icon
        ImageView expIconControl = new ImageView(context);
        LinearLayout.LayoutParams expIconParams = new LinearLayout.LayoutParams(Math.round(0.14583f * width),
                                                                                LayoutParams.WRAP_CONTENT, 0.2f);
        expIconParams.setMargins(1, 1, 1, 1);
        expIconControl.setImageBitmap(exposure.getEvStepBtmp());
        addView(expIconControl, expIconParams);
    }

}


/**
 * Exposure results adapter.
 */
public class ExposureAdapter extends BaseAdapter {

    private static DisplayMetrics metrics;
    private Context context;
    private List<Exposure> exposureList;


    /**
     * Creates a new ExposureAdapter object.
     */
    public ExposureAdapter(Context context, List<Exposure> expList) {
        this.context = context;
        this.exposureList = expList;
    }

    /**
     * Retrieves list size.
     *
     * @return  list size
     */
    @Override
    public int getCount() {
        return this.exposureList.size();
    }

    /**
     * Retrieves the exposure at index <code>position</code>.
     *
     * @param   position  Index in list
     *
     * @return  Exposure at index <code>position</code>
     */
    @Override
    public Object getItem(int position) {
        return this.exposureList.get(position);
    }

    /**
     * Retrieves the number of the exposure at index <code>position</code>.
     *
     * @param   position  Index in list
     *
     * @return  the number of the exposure at index <code>position</code>
     */
    @Override
    public long getItemId(int position) {

        //return this.exposureList.get(position).getNbr();
        return position;
    }

    /**
     * Retrieves a view representing a single result row.
     *
     * @param   position     Index in list
     * @param   convertView  view
     * @param   parent       parent
     *
     * @return  {@link ExposureAdapterView}
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Exposure exposure = this.exposureList.get(position);
        return new ExposureAdapterView(this.context, exposure);
    }

    /**
     * Retrieves the value of
     *
     * @return  DOCUMENT ME!
     */
    public static View getHeaderView(Context context, LinearLayout headerLL) {

        int black = headerLL.getResources().getColor(R.color.black);
        int width = getMetrics(context).widthPixels;

        // Nbr
        TextView nbrControl = new TextView(context);
        LinearLayout.LayoutParams nbrParams = new LinearLayout.LayoutParams(Math.round(0.14583f * width),
                                                                            LayoutParams.WRAP_CONTENT, 0.2f);
        nbrParams.setMargins(3, 1, 1, 1);
        nbrControl.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        nbrControl.setTextSize(13f);
        nbrControl.setTextColor(black);
        nbrControl.setText(R.string.expNbrLbl);
        headerLL.addView(nbrControl, nbrParams);

        // Aperture
        TextView apertureControl = new TextView(context);
        LinearLayout.LayoutParams apertureParams = new LinearLayout.LayoutParams(Math.round(0.20833f * width),
                                                                                 LayoutParams.WRAP_CONTENT, 0.2f);
        apertureParams.setMargins(1, 1, 1, 1);
        apertureControl.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        apertureControl.setTextSize(13f);
        apertureControl.setTextColor(black);
        apertureControl.setText(R.string.aperture);
        headerLL.addView(apertureControl, apertureParams);

        // Shutter speed
        TextView shutterControl = new TextView(context);
        LinearLayout.LayoutParams shutterParams = new LinearLayout.LayoutParams(Math.round(0.35417f * width),
                                                                                LayoutParams.WRAP_CONTENT, 0.2f);
        shutterParams.setMargins(1, 1, 1, 1);
        shutterControl.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        shutterControl.setTextSize(13f);
        shutterControl.setTextColor(black);
        shutterControl.setText(R.string.shutter_speed);
        headerLL.addView(shutterControl, shutterParams);

        // EV Step
        TextView evStepControl = new TextView(context);
        LinearLayout.LayoutParams evStepParams = new LinearLayout.LayoutParams(Math.round(0.14583f * width),
                                                                               LayoutParams.WRAP_CONTENT, 0.2f);
        evStepParams.setMargins(1, 1, 1, 1);
        evStepControl.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        evStepControl.setTextSize(13f);
        evStepControl.setTextColor(black);
        evStepControl.setText(R.string.expEVStep);
        headerLL.addView(evStepControl, evStepParams);

        // Icon
        ImageView expIconControl = new ImageView(context);
        LinearLayout.LayoutParams expIconParams = new LinearLayout.LayoutParams(Math.round(0.14583f * width),
                                                                                LayoutParams.WRAP_CONTENT, 0.2f);
        expIconParams.setMargins(1, 1, 1, 1);
        headerLL.addView(expIconControl, expIconParams);

        return headerLL;
    }

    /**
     * Retrieves the value of
     *
     * @return  DOCUMENT ME!
     */
    private static DisplayMetrics getMetrics(Context context) {

        if (metrics == null) {
            metrics = new DisplayMetrics();
            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        }

        return metrics;
    }

}
