package com.turlir.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.turlir.breaklayout.BreakLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends Activity implements SettingsDialog.Callback {

    private static final int[] MODELS = new int[] {
            BreakLayout.MODE_RIGHT,
            BreakLayout.MODE_LEFT,
            BreakLayout.MODE_CENTER,
            BreakLayout.MODE_EDGE,
            BreakLayout.MODE_AS_IS,
    };

    private static final String BUNDLE_MODEL = "BUNDLE_MODEL";

    @BindView(R.id.target)
    BreakLayout target;

    @Override
    protected void onCreate(Bundle state) {
        super.onCreate(state);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        if (state != null) {
            newMode((Model) state.getParcelable(BUNDLE_MODEL));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Model m = new Model(target.getMode(), target.getChildCount());
        outState.putParcelable(BUNDLE_MODEL, m);
        super.onSaveInstanceState(outState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.settings) {
            openSettingsDialog();
        }
        return false;
    }

    private void openSettingsDialog() {
        Model current = new Model(target.getMode(), target.getChildCount());
        SettingsDialog frg = SettingsDialog.newInstance(MODELS, current);
        frg.show(getFragmentManager(), null);
    }

    @Override
    public void newMode(Model n) {
        int m = n.getId();
        int c = n.getCount();
        // old algorithm
        int tmp = target.getMode();
        if (c > target.getChildCount()) {
            View childAt = target.getChildAt(0);
            for (int i = 0; i <= c - target.getChildCount(); i++) {
                View v = new View(getApplicationContext());
                v.setLayoutParams(childAt.getLayoutParams());
                v.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                target.addView(v);
            }
        } else if (c < target.getChildCount()) {
            for (int i = target.getChildCount() - 1; i >= c; i--) {
                target.removeViewAt(i);
            }
        }
        if (tmp != m) {
            target.setMode(m);
        }
    }

}

@@ -1935,40 +1935,13 @@
 del 
-n
+m
 ) %7B%0A
-        int m = n.getId();%0A
     
@@ -1952,17 +1952,17 @@
 int c = 
-n
+m
 .getCoun
@@ -1970,69 +1970,8 @@
 ();%0A
-        // old algorithm%0A        int tmp = target.getMode();%0A
     
@@ -1999,32 +1999,39 @@
 tChildCount()) %7B
+ // add
 %0A            Vie
@@ -2080,27 +2080,17 @@
     
-for (int i = 0; i %3C
+int diff 
 = c 
@@ -2118,12 +2118,50 @@
 t();
+%0A            for (int i = 0; i %3C diff;
  
-i
 ++
+i
 ) %7B%0A
@@ -2464,16 +2464,26 @@
 unt()) %7B
+ // remove
 %0A       
@@ -2487,26 +2487,24 @@
         
-for (int i
+int diff
  = targe
@@ -2527,22 +2527,52 @@
 ) - 
-1; i %3E= c; i--
+c;%0A            for (int i = 0; i %3C diff; ++i
 ) %7B%0A
@@ -2611,11 +2611,40 @@
 wAt(
-i);
+(diff + c) - i - 1); // from end
 %0A   
@@ -2676,20 +2676,119 @@
     
-if (tmp != m
+// else count not change - nothing%0A        // mode change independent%0A        if (target.getMode() != m.getId()
 ) %7B%0A
@@ -2815,16 +2815,24 @@
 etMode(m
+.getId()
 );%0A     

Fix append, remove view algorithm, add comments