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.construction.SectionBuilder;
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() {
        // create sections
        section1 = new HomogeneousSection<>(SampleModel.class, SampleModelViewCell.class);
        section2 = new HomogeneousSection<>(SampleModel.class, SampleModelViewCell.class);

        return new ViewCellAdapter().addSection(
            SectionBuilder.compositeSectionBuilder()
                .addSection(
                    SectionBuilder.wrap(section1)
                        .wrapWithHeader(new HeaderViewCell("Section 1"))
                        .hideHeaderIfEmpty()
                        .build()
                )
                .addSection(
                    SectionBuilder.wrap(section2)
                        .wrapWithHeader(new HeaderViewCell("Section 2"))
                        .hideHeaderIfEmpty()
                        .build()
                )
                .wrapWithEmptyView(new EmptyViewCell("Add items at the top"))
                .build()
        )
        .addListener(new SampleModelViewCell.OnSampleModelClickListener() {
            @Override
            public void onSampleModelClick(SampleModel sampleModel) {
                String snackMessage = String.format(Locale.getDefault(), "%s was clicked!", sampleModel.getName());
                showSnackbar(snackMessage);
            }
        });
    }

    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);
        }
    }
}

@@ -1697,16 +1697,29 @@
 dapter()
+%0A            
 .addSect
@@ -1727,32 +1727,36 @@
 on(%0A            
+ 
+   
 SectionBuilder.c
@@ -1776,24 +1776,28 @@
 onBuilder()%0A
+    
             
@@ -1825,32 +1825,36 @@
                 
+    
 SectionBuilder.w
@@ -1883,32 +1883,36 @@
                 
+  
+  
 .wrapWithHeader(
@@ -1960,32 +1960,36 @@
                 
+    
 .hideHeaderIfEmp
@@ -2009,32 +2009,36 @@
                 
+    
 .build()%0A       
@@ -2038,34 +2038,42 @@
                 
+   
+ 
 )%0A
+    
                 
@@ -2077,32 +2077,36 @@
    .addSection(%0A
+    
                 
@@ -2139,16 +2139,20 @@
 ction2)%0A
+    
         
@@ -2232,32 +2232,36 @@
                 
+    
 .hideHeaderIfEmp
@@ -2257,32 +2257,36 @@
 HeaderIfEmpty()%0A
+    
                 
@@ -2310,32 +2310,36 @@
                 
+    
 )%0A              
@@ -2332,32 +2332,36 @@
                 
+ 
+   
 .wrapWithEmptyVi
@@ -2414,32 +2414,36 @@
                 
+    
 .build()%0A       
@@ -2443,26 +2443,32 @@
         
-)%0A
+    )%0A  
         
 .addList
@@ -2455,24 +2455,26 @@
 )%0A          
+  
 .addListener
@@ -2537,24 +2537,28 @@
             
+    
 @Override%0A  
@@ -2563,24 +2563,28 @@
             
+ 
+   
 public void 
@@ -2637,32 +2637,36 @@
                 
+   
+ 
 String snackMess
@@ -2765,16 +2765,20 @@
         
+    
 showSnac
@@ -2805,26 +2805,34 @@
             
-%7D%0A
+    %7D%0A    
         %7D);%0A

Fix indentation