package ca.antonious.sample.about;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import ca.antonious.sample.BaseActivity;
import ca.antonious.sample.R;
import ca.antonious.sample.viewcells.HeaderViewCell;
import ca.antonious.viewcelladapter.ViewCellAdapter;
import ca.antonious.viewcelladapter.decorators.HeaderSectionDecorator;
import ca.antonious.viewcelladapter.sections.HomogeneousSection;

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

public class AboutActivity extends BaseActivity {
    private ViewCellAdapter viewCellAdapter;
    private HomogeneousSection<Library, LibraryViewCell> librariesSection;

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

        viewCellAdapter = buildAdapter();

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

        librariesSection.setAll(getLibraries());
    }

    private ViewCellAdapter buildAdapter() {
        librariesSection = new HomogeneousSection<>(Library.class, LibraryViewCell.class);

        HeaderViewCell librariesHeader = new HeaderViewCell("Libraries Used");
        HeaderSectionDecorator headerSectionDecorator = new HeaderSectionDecorator(librariesSection, librariesHeader);
        headerSectionDecorator.setShowHeaderIfEmpty(false);

        viewCellAdapter.add(headerSectionDecorator);

        viewCellAdapter.addListener(new LibraryViewCell.OnLibraryClickListener() {
            @Override
            public void onLibraryClick(Library library) {
                openLibraryLink(library);
            }
        });

        return viewCellAdapter;
    }

    private void openLibraryLink(Library library) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(library.getSourceControlUrl()));
        startActivity(browserIntent);
    }

    public List<Library> getLibraries() {
        List<Library> libraries = new ArrayList<>();

        for (String library: getLibraryStringArray()) {
            libraries.add(deserializeLibrary(library));
        }

        return libraries;
    }

    private String[] getLibraryStringArray() {
        try {
            return getResources().getStringArray(R.array.libraries);
        } catch (Exception e) {
            return new String[]{};
        }
    }

    private Library deserializeLibrary(String serializedLibrary) {
        String[] libraryTokens = serializedLibrary.split("======");
        return new Library(libraryTokens[0].trim(), libraryTokens[1].trim());
    }
}

@@ -394,16 +394,81 @@
 dapter;%0A
+import ca.antonious.viewcelladapter.construction.SectionBuilder;%0A
 import c
@@ -1293,30 +1293,31 @@
         
-Header
+return 
 ViewCell
  librari
@@ -1312,327 +1312,273 @@
 Cell
- librariesHeader = new HeaderViewCell(%22Libraries Used%22);%0A        HeaderSectionDecorator headerSectionDecorator = new HeaderSectionDecorator(librariesSection, librariesHeader);%0A        headerSectionDecorator.setShowHeaderIfEmpty(false);%0A%0A        viewCellAdapter.add(headerSectionDecorator);%0A%0A        viewCellAdapter.addL
+Adapter.create()%0A            .section(%0A                SectionBuilder.wrap(librariesSection)%0A                    .wrapWithHeader(new HeaderViewCell(%22Libraries Used%22))%0A                    .ifEmptyHideHeader()%0A                    .build()%0A            )%0A            .l
 iste
@@ -1624,24 +1624,28 @@
 istener() %7B%0A
+    
             
@@ -1662,24 +1662,28 @@
             
+    
 public void 
@@ -1732,16 +1732,20 @@
         
+  
+  
 openLibr
@@ -1778,53 +1778,49 @@
     
-%7D%0A        %7D);%0A%0A        return viewCellAdapter
+    %7D%0A            %7D)%0A            .build()
 ;%0A  

Fix libraries screen