/*
 * Copyright (C) 2014 Agens AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package no.agens.cassowarylayoutdemo;

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

import no.agens.cassowarylayout.CassowaryLayout;
import no.agens.cassowarylayout.ViewIdResolver;

public class ProgrammaticDemo extends Activity {

    private static String[] CONSTRAINTS = {
            "blue.left >= 100dp",
            "blue.top >= 10dp",
            "blue.width == 120dp",
            "blue.height == (blue.width + 200dp) / 2",
            "green.x >= blue.x + blue.width + 10dp",
            "green.y >= 10dp",
            "green.width == blue.width",
            "green.height == blue.height",
            "red.width == blue.width * 2px",
            "red.height == blue.height * 2px",
            "red.x >= 10dp",
            "red.y >= green.y + green.height + 10dp"
    };

    private CassowaryLayout cassowaryLayout;

    private View red;
    private View green;
    private View blue;

    private int nextId = 1000;

    private ViewIdResolver viewIdResolver = new ViewIdResolver() {

        @Override
        public int getViewIdByName(String viewName) {
            if ("red".equals(viewName)) {
                return red.getId();
            } else if ("blue".equals(viewName)) {
                return blue.getId();
            } else if ("green".equals(viewName)) {
                return green.getId();
            }
            return 0;
        }

        @Override
        public String getViewNameById(int id) {
           if (id == red.getId()) {
               return "red";
           } else if (id == blue.getId()) {
               return "blue";
           } else if (id == green.getId()) {
               return "green";
           }
           return "";
        }
    };



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

        cassowaryLayout = new CassowaryLayout(this, viewIdResolver);

        red = new View(this);
        // Each child view *MUST* have a unique ID
        red.setId(nextId++);
        red.setBackgroundColor(getResources().getColor(R.color.red));
        blue = new View(this);
        blue.setId(nextId++);
        blue.setBackgroundColor(getResources().getColor(R.color.blue));
        green = new View(this);
        green.setId(nextId++);
        green.setBackgroundColor(getResources().getColor(R.color.green));

        cassowaryLayout.addView(red);
        cassowaryLayout.addView(green);
        cassowaryLayout.addView(blue);
        cassowaryLayout.getCassowaryModel().addConstraints(CONSTRAINTS);
        setContentView(cassowaryLayout);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.programmatic_demo, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -982,21 +982,14 @@
 lue.
-left %3E= 100dp
+x == 0
 %22,%0A 
@@ -1009,19 +1009,14 @@
 lue.
-top %3E= 10dp
+y == 0
 %22,%0A 
@@ -1042,17 +1042,17 @@
 dth == 1
-2
+0
 0dp%22,%0A  
@@ -1077,17 +1077,16 @@
 ight == 
-(
 blue.wid
@@ -1091,21 +1091,8 @@
 idth
- + 200dp) / 2
 %22,%0A 
@@ -1113,39 +1113,27 @@
 een.
-x %3E= blue.x + blue.width + 10dp
+width == blue.width
 %22,%0A 
@@ -1154,17 +1154,29 @@
 een.
-y %3E= 10dp
+height == blue.height
 %22,%0A 
@@ -1185,37 +1185,33 @@
          %22green.
-width
+x
  == blue.width%22,
@@ -1195,37 +1195,34 @@
 green.x == blue.
-width
+y2
 %22,%0A            %22
@@ -1219,38 +1219,33 @@
          %22green.
-height
+y
  == blue.height%22
@@ -1229,38 +1229,34 @@
 green.y == blue.
-height
+y2
 %22,%0A            %22
@@ -1272,24 +1272,19 @@
  == 
-blue
+green
 .width
- * 2px
 %22,%0A 
@@ -1313,25 +1313,20 @@
  == 
-blue
+green
 .height
- * 2px
 %22,%0A 
@@ -1347,15 +1347,19 @@
 d.x 
-%3E= 10dp
+== green.x2
 %22,%0A 
@@ -1380,9 +1380,9 @@
 d.y 
-%3E
+=
 = gr
@@ -1390,30 +1390,9 @@
 en.y
- + green.height + 10dp
+2
 %22%0A  

Use 'stairs' constraints for Programmatic demo