package com.example.android.sunshine.app;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import static com.example.android.sunshine.app.R.id.list_item_temp_max;

/**
 * {@link ForecastAdapter} exposes a list of weather forecasts
 * from a {@link android.database.Cursor} to a {@link android.widget.ListView}.
 */
public class ForecastAdapter extends CursorAdapter {
    public ForecastAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    /**
     * Prepare the weather high/lows for presentation.
     */
    private String formatHighLows(double high, double low) {
        boolean isMetric = Utility.isMetric(mContext);
        String highLowStr = Utility.formatTemperature(high, isMetric) + "/" + Utility.formatTemperature(low, isMetric);
        return highLowStr;
    }

    /*
        This is ported from FetchWeatherTask --- but now we go straight from the cursor to the
        string.
     */
    private String convertCursorRowToUXFormat(Cursor cursor) {
//        // get row indices for our cursor
//        int idx_max_temp = cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_MAX_TEMP);
//        int idx_min_temp = cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_MIN_TEMP);
//        int idx_date = cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_DATE);
//        int idx_short_desc = cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_SHORT_DESC);
        // Use projection instead.
        String highAndLow = formatHighLows(
                cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP),
                cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP));

        return Utility.formatDate(cursor.getLong(ForecastFragment.COL_WEATHER_DATE)) +
                " - " + cursor.getString(ForecastFragment.COL_WEATHER_DESC) +
                " - " + highAndLow;
    }

    /*
        Remember that these views are reused as needed.
     */
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item_forecast, parent, false);

        return view;
    }

    /*
        This is where we fill-in the views with the contents of the cursor.
     */
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // our view is pretty simple here --- just a text view
        // we'll keep the UI functional with a simple (and slow!) binding.

        // Read weather icon ID from cursor
        int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_ID);
        // Use placeholder image for now
        ImageView iconView = (ImageView) view.findViewById(R.id.list_item_icon);
        iconView.setImageResource(R.drawable.ic_launcher);

        // Read date from cursor
        String date = Utility.getFriendlyDayString(context, cursor.getLong(ForecastFragment.COL_WEATHER_DATE));
        TextView dateView = (TextView)view.findViewById(R.id.list_item_date);
        dateView.setText(date);
        // Read weather forecast from cursor
        String forecast = cursor.getString(ForecastFragment.COL_WEATHER_DESC);
        TextView forecastView = (TextView)view.findViewById(R.id.list_item_forecast);
        forecastView.setText(forecast);

        // Read user preference for metric or imperial temperature units
        boolean isMetric = Utility.isMetric(context);

        // Read high temperature from cursor
        double high = cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP);
        TextView highView = (TextView) view.findViewById(list_item_temp_max);
        highView.setText(Utility.formatTemperature(high, isMetric));

        // Read low temperature from cursor
        double low = cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP);
        TextView lowView = (TextView) view.findViewById(R.id.list_item_temp_min);
        lowView.setText(Utility.formatTemperature(low, isMetric));
    }
}
@@ -584,16 +584,108 @@
 apter %7B%0A
+    private final int VIEW_TYPE_TODAY = 0;%0A    private final int VIEW_TYPE_FUTURE_DAY = 1;%0A%0A
     publ
@@ -739,24 +739,24 @@
 nt flags) %7B%0A
-
         supe
@@ -2338,24 +2338,220 @@
 p parent) %7B%0A
+        // Choose the layout type from the cursor position.%0A        int viewType = getItemViewType(cursor.getPosition());%0A        int layoutId = -1;%0A        // Determine layoutId from viewType.%0A//
         View
@@ -2640,17 +2640,18 @@
 false);%0A
-%0A
+//
         
@@ -2663,16 +2663,369 @@
 n view;%0A
+        switch (viewType) %7B%0A            case VIEW_TYPE_TODAY:%0A                layoutId = R.layout.list_item_forecast_today;%0A                break;%0A            case VIEW_TYPE_FUTURE_DAY:%0A                layoutId = R.layout.list_item_forecast;%0A                break;%0A        %7D%0A        return LayoutInflater.from(context).inflate(layoutId, parent, false);%0A
     %7D%0A%0A 
@@ -4741,24 +4741,24 @@
 _temp_min);%0A
-
         lowV
@@ -4817,10 +4817,225 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public int getItemViewType(int position) %7B%0A        return (position == 0) ? VIEW_TYPE_TODAY: VIEW_TYPE_FUTURE_DAY;%0A    %7D%0A%0A    @Override%0A    public int getViewTypeCount() %7B%0A        return 2;%0A    %7D
 %0A%7D

Use two item view types for forecast layout.