package it.polimi.dima.giftlist.presentation.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import hugo.weaving.DebugLog;
import it.polimi.dima.giftlist.R;
import it.polimi.dima.giftlist.di.HasComponent;
import it.polimi.dima.giftlist.presentation.component.ProductListComponent;
import it.polimi.dima.giftlist.presentation.module.ProductListModule;
import it.polimi.dima.giftlist.presentation.view.fragment.ProductListFragment;

/**
 * Created by Elena on 19/01/2016.
 */
public class ProductListActivity extends BaseActivity implements HasComponent<ProductListComponent> {

    private static final String EXTRA_CATEGORY_SELECTED = "category";
    private static final String EXTRA_KEYWORDS = "keywords";

    private String category;
    private String keywords;

    //The component has to be declared in the activity and not in the fragment since its lifecycle must be tied to the activity
    ProductListComponent productListComponent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wishlistlist);
        if (savedInstanceState == null) {
            category = getIntent().getStringExtra(EXTRA_CATEGORY_SELECTED);
            keywords = getIntent().getStringExtra(EXTRA_KEYWORDS);
            createComponent();
            Bundle args = new Bundle();
            args.putString(EXTRA_CATEGORY_SELECTED,category);
            args.putString(EXTRA_KEYWORDS,keywords);
            addFragment(R.id.activity_frame, new ProductListFragment(), args);
        }
    }

    @DebugLog
    public static Intent getCallingIntent(Context context, String category, String keywords) {
        Intent callingIntent = new Intent(context, ProductListActivity.class);
        callingIntent.putExtra(EXTRA_CATEGORY_SELECTED, category);
        callingIntent.putExtra(EXTRA_KEYWORDS, keywords);
        return callingIntent;
    }

    //I need to expose to component so that I can perform injection from the fragment
    @Override
    public ProductListComponent getComponent() {
        return productListComponent;
    }

    protected void createComponent() {
        productListComponent = getApplicationComponent().plus(new ProductListModule(this, category, keywords));
    }
}

@@ -1215,16 +1215,309 @@
 tlist);%0A
+        //I need category and keywords both for createComponent() and to launch the fragment%0A        //If it's the first time creating the activity, I get them from the Intent.%0A        //If the activity is recreated e.g. after rotation, they are restored by IcePick in the super.onCreate call%0A
         
@@ -1546,24 +1546,24 @@
  == null) %7B%0A
-
             
@@ -1693,24 +1693,30 @@
 WORDS);%0A    
+    %7D%0A
         crea
@@ -1726,24 +1726,66 @@
 omponent();%0A
+        if (savedInstanceState == null) %7B%0A
             
@@ -2002,24 +2002,24 @@
 t(), args);%0A
-
         %7D%0A  
@@ -2012,24 +2012,25 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%0A    @

Fix the ProductListComponent not being recreated after rotation