package com.ae.apps.tripmeter.fragments;

import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ae.apps.tripmeter.R;
import com.ae.apps.tripmeter.database.TripExpensesDatabase;
import com.ae.apps.tripmeter.listeners.ExpensesInteractionListener;
import com.ae.apps.tripmeter.managers.ExpenseContactManager;
import com.ae.apps.tripmeter.models.Trip;
import com.ae.apps.tripmeter.models.TripExpense;
import com.ae.apps.tripmeter.utils.AppConstants;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Activities that contain this fragment must implement the
 * {@link ExpensesInteractionListener} interface
 * to handle interaction events.
 */
public class TripDetailsFragment extends Fragment
        implements AddExpenseDialogFragment.AddExpenseDialogListener{

    private long mTripId;
    private Trip mTrip;
    private TripExpensesDatabase mExpensesDatabase;
    private ExpenseContactManager mContactManager;
    private ExpensesInteractionListener mListener;

    public TripDetailsFragment() {
        // Required empty public constructor
    }

    /**
     * @return A new instance of fragment TripDetailsFragment.
     */
    public static TripDetailsFragment newInstance() {
        return new TripDetailsFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflatedView = inflater.inflate(R.layout.fragment_trip_details, container, false);

        if (null == getArguments() && null == savedInstanceState) {
            throw new IllegalArgumentException("TripId is required");
        }

        if(null != getArguments()){
            mTripId = getArguments().getLong(AppConstants.KEY_TRIP_ID);
        }

        if(null != savedInstanceState){
            mTripId = savedInstanceState.getLong(AppConstants.KEY_TRIP_ID);
        }

        mExpensesDatabase = new TripExpensesDatabase(getActivity());
        mTrip = mExpensesDatabase.getTrip(mTripId);

        // Update the trip with the ContactVos from member ids
        mContactManager = ExpenseContactManager.newInstance(getActivity().getContentResolver());
        mTrip.getMembers().addAll(mContactManager.getContactsFromIds(mTrip.getMemberIds()));

        TextView tripName = (TextView) inflatedView.findViewById(R.id.txtTripName);
        TextView tripDate = (TextView) inflatedView.findViewById(R.id.txtTripDate);

        tripName.setText(mTrip.getName());

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(mTrip.getStartDate());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(AppConstants.TRIP_DATE_FORMAT);
        tripDate.setText(simpleDateFormat.format(calendar.getTime()));

        FloatingActionButton floatingActionButton = (FloatingActionButton) inflatedView.findViewById(R.id.fab);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddExpenseDialog();
            }
        });

        return inflatedView;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(AppConstants.KEY_TRIP_ID, mTripId);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof ExpensesInteractionListener) {
            mListener = (ExpensesInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement ExpensesInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     *
     * See https://guides.codepath.com/android/Using-DialogFragment#passing-data-to-parent-fragment
     */
    private void showAddExpenseDialog(){
        FragmentManager fragmentManager = getFragmentManager();
        AddExpenseDialogFragment dialogFragment = AddExpenseDialogFragment.newInstance(mTrip);
        dialogFragment.setTargetFragment(TripDetailsFragment.this, 300);
        dialogFragment.show(fragmentManager, "fragment_add_expense");
    }

    @Override
    public void onExpenseAdded(TripExpense tripExpense) {
        // TODO Ask TripExpenseManager to add this expense and calculate the share
    }
}

@@ -598,68 +598,8 @@
 .R;%0A
-import com.ae.apps.tripmeter.database.TripExpensesDatabase;%0A
 impo
@@ -703,31 +703,24 @@
 gers.Expense
-Contact
 Manager;%0Aimp
@@ -1257,90 +1257,31 @@
 ate 
-Trip
 Expense
-sDatabase mExpensesDatabase;%0A    private ExpenseContactManager mContact
+Manager mExpense
 Mana
@@ -2432,44 +2432,36 @@
 ense
-sDatabase
+Manager
  = new 
-Trip
 Expense
-sDatabase
+Manager
 (get
@@ -2501,25 +2501,31 @@
 ense
-sDatabase.getTrip
+Manager.getTripByTripId
 (mTr
@@ -2599,105 +2599,8 @@
 ids%0A
-        mContactManager = ExpenseContactManager.newInstance(getActivity().getContentResolver());%0A
     
@@ -2630,23 +2630,23 @@
 addAll(m
-Contact
+Expense
 Manager.
@@ -4825,16 +4825,40 @@
 e share%0A
+        mExpenseManager%0A
     %7D%0A%7D%0A

Use ExpenseManager