package ca.antonious.sample;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Arrays;
import java.util.Locale;

import ca.antonious.sample.models.SampleModel;
import ca.antonious.sample.viewcells.EmptyViewCell;
import ca.antonious.sample.viewcells.HeaderViewCell;
import ca.antonious.sample.viewcells.SampleModelViewCell;
import ca.antonious.viewcelladapter.ViewCellAdapter;
import ca.antonious.viewcelladapter.decorators.EmptySectionDecorator;
import ca.antonious.viewcelladapter.decorators.HeaderSectionDecorator;
import ca.antonious.viewcelladapter.sections.CompositeSection;
import ca.antonious.viewcelladapter.sections.HomogeneousSection;

/**
 * Created by George on 2017-01-08.
 */

public class ComplexDecoratorCompositionSample extends BaseActivity {
    private ViewCellAdapter viewCellAdapter;
    private HomogeneousSection<SampleModel, SampleModelViewCell> section1;
    private HomogeneousSection<SampleModel, SampleModelViewCell> section2;

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

        viewCellAdapter = buildAdapter();

        recyclerView.setAdapter(viewCellAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private ViewCellAdapter buildAdapter() {
        ViewCellAdapter viewCellAdapter = new ViewCellAdapter();
        viewCellAdapter.setHasStableIds(true);

        // create sections
        section1 = new HomogeneousSection<>(SampleModel.class, SampleModelViewCell.class);
        section2 = new HomogeneousSection<>(SampleModel.class, SampleModelViewCell.class);

        // decorate section 1 with a header
        HeaderViewCell section1Header = new HeaderViewCell("Section 1");
        HeaderSectionDecorator section1WithHeader = new HeaderSectionDecorator(section1, section1Header);
        section1WithHeader.setShowHeaderIfEmpty(false);

        // decorate section 2 with a header
        HeaderViewCell section2Header = new HeaderViewCell("Section 2");
        HeaderSectionDecorator section2WithHeader = new HeaderSectionDecorator(section2, section2Header);
        section2WithHeader.setShowHeaderIfEmpty(false);

        // combine decorated sections into single composite section
        CompositeSection compositeSection = new CompositeSection()
                .addSection(section1WithHeader)
                .addSection(section2WithHeader);

        // decorate composite section with an empty view
        EmptyViewCell emptyView = new EmptyViewCell("Add items at the top");
        EmptySectionDecorator compositeSectionWithEmptyView = new EmptySectionDecorator(compositeSection, emptyView);

        // add decorated composite section
        viewCellAdapter.add(compositeSectionWithEmptyView);

        // register on sample model clicked listener
        viewCellAdapter.addListener(new SampleModelViewCell.OnSampleModelClickListener() {
            @Override
            public void onSampleModelClick(SampleModel sampleModel) {
                String snackMessage = String.format(Locale.getDefault(), "%s was clicked!", sampleModel.getName());
                showSnackbar(snackMessage);
            }
        });

        return viewCellAdapter;
    }

    private void prependSampleModelToSection1() {
        String sampleModelName = String.format(Locale.getDefault(), "%s Test %d", Utils.getRandomLetter(), section1.getItemCount());
        section1.prependAll(Arrays.asList(new SampleModel(sampleModelName)));
        viewCellAdapter.notifyDataSetChanged();
    }

    private void clearSection1() {
        section1.clear();
        viewCellAdapter.notifyDataSetChanged();
    }

    private void prependSampleModelToSection2() {
        String sampleModelName = String.format(Locale.getDefault(), "%s Test %d", Utils.getRandomLetter(), section2.getItemCount());
        section2.prependAll(Arrays.asList(new SampleModel(sampleModelName)));
        viewCellAdapter.notifyDataSetChanged();
    }

    private void clearSection2() {
        section2.clear();
        viewCellAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.complex_section_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_section1:
                prependSampleModelToSection1();
                return true;
            case R.id.action_add_section2:
                prependSampleModelToSection2();
                return true;
            case R.id.action_clear_section1:
                clearSection1();
                return true;
            case R.id.action_clear_section2:
                clearSection2();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

@@ -470,24 +470,89 @@
 ellAdapter;%0A
+import ca.antonious.viewcelladapter.construction.SectionBuilder;%0A
 import ca.an
@@ -601,32 +601,32 @@
 ctionDecorator;%0A
-
 import ca.antoni
@@ -1455,121 +1455,8 @@
 ) %7B%0A
-        ViewCellAdapter viewCellAdapter = new ViewCellAdapter();%0A        viewCellAdapter.setHasStableIds(true);%0A%0A
     
@@ -1673,116 +1673,48 @@
     
-// decorate section 1 with a header%0A        HeaderViewCell section1Header = new HeaderViewCell(%22
+return new ViewCellAdapter().add
 Section
- 1%22);
+(
 %0A   
@@ -1722,114 +1722,102 @@
     
-Header
+    
 Section
-Decorator section1WithHeader = new HeaderSectionDecorator(section1, section1Header);%0A
+Builder.compositeSectionBuilder()%0A                .addSection(%0A            
         
 sect
@@ -1816,141 +1816,78 @@
     
-s
+S
 ection
-1WithHeader.setShowHeaderIfEmpty(false);%0A%0A        // decorate section 2 with a header%0A        HeaderViewCell section2
+Builder.wrap(section1)%0A                        .wrapWith
 Header
- = 
+(
 new 
@@ -1914,12 +1914,12 @@
 ion 
-2%22);
+1%22))
 %0A   
@@ -1927,296 +1927,94 @@
     
-HeaderSectionDecorator section2WithHeader = new HeaderSectionDecorator(section2, section2Header);%0A        section2WithHeader.setShowHeaderIfEmpty(false);%0A%0A        // combine decorated sections into single composite section%0A        CompositeSection compositeSection = new CompositeSection(
+                .hideHeaderIfEmpty()%0A                        .build()%0A                
 )%0A  
@@ -2043,27 +2043,8 @@
 ion(
-section1WithHeader)
 %0A   
@@ -2060,222 +2060,232 @@
     
-.add
+    
 Section
-(section2WithHeader);%0A%0A        // decorate composite section with an empty view%0A        EmptyViewCell emptyView = new EmptyViewCell(%22Add items at the top%22);%0A        EmptySectionDecorator compositeSection
+Builder.wrap(section2)%0A                        .wrapWithHeader(new HeaderViewCell(%22Section 2%22))%0A                        .hideHeaderIfEmpty()%0A                        .build()%0A                )%0A                .wrap
 With
@@ -2293,19 +2293,17 @@
 mptyView
- = 
+(
 new Empt
@@ -2307,236 +2307,85 @@
 mpty
-SectionDecorator(compositeSection, emptyView);%0A%0A        // add decorated composite section%0A        viewCellAdapter.add(compositeSectionWithEmptyView);%0A%0A        // register on sample model clicked listener%0A        viewCellAdapter
+ViewCell(%22Add items at the top%22))%0A                .build()%0A        )%0A        
 .add
@@ -2710,24 +2710,24 @@
           %7D%0A
+
         %7D);%0A
@@ -2729,41 +2729,8 @@
  %7D);
-%0A%0A        return viewCellAdapter;
 %0A   

Update ComplexDecorator example to use new builder construction method