package io.fabianterhorst.fastlayout.converters;

import java.util.List;

import io.fabianterhorst.fastlayout.annotations.Converter;

/**
 * Created by fabianterhorst on 18.05.16.
 */
@Converter
public class LayoutConverter {

    public LayoutAttribute convert(Object attributeValue, String attributeName) {
        return onConvertLayoutAttributeValue(attributeValue, attributeName);
    }

    public List<LayoutAttribute> finish() {
        return onFinish();
    }

    public LayoutAttribute onConvertLayoutAttributeValue(Object attributeValue, String attributeName) {
        String attribute = String.valueOf(attributeValue);
        LayoutAttribute layoutAttribute = new LayoutAttribute(attributeValue);
        if (attribute.startsWith("@+id/") || attribute.startsWith("@id/")) {
            return onConvertLayoutAttribute("R.id" + attribute.replace("@+id/", ".").replace("@id/", "."), attributeName, false);
        } else if (attribute.startsWith("@dimen/")) {
            return onConvertLayoutAttribute("(int) getContext().getResources().getDimension(R.dimen." + attribute.replace("@dimen/", "") + ")", attributeName, false);
        } else if (attribute.startsWith("@string/")) {
            return onConvertLayoutAttribute("getContext().getString(R.string." + attribute.replace("@string/", "") + ")", attributeName, false);
        } else if (attribute.endsWith("dp") && isNumber(attribute.replace("dp", ""))) {
            return onConvertLayoutAttribute("(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, " + attribute.replace("dp", "") + ", getResources().getDisplayMetrics())", attributeName, false);
        } /*else if (attribute.startsWith("?attr/") && attributeName != null && attributeName.equals("Background")) {
            layoutAttribute = new LayoutAttribute("LayoutUtils.getAttrDrawable(getContext(), R.attr." + attribute.replace("?attr/", "") + ")", false);
        } else if (attribute.startsWith("?attr/")) {
            layoutAttribute = new LayoutAttribute("LayoutUtils.getAttrInt(getContext(), R.attr." + attribute.replace("?attr/", "") + ")", false);
        } */ else if (attribute.equals("false") || attribute.equals("true")) {
            return onConvertLayoutAttribute(attribute, attributeName, false);
        } else if (attribute.endsWith("sp") && isNumber(attribute.replace("sp", ""))) {
            return onConvertLayoutAttribute("(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, " + attribute.replace("sp", "") + ", Resources.getSystem().getDisplayMetrics())", attributeName, false);
        } else if (isNumber(attribute)) {
            return onConvertLayoutAttribute(attributeValue, attributeName, false);
        }
        return onConvertLayoutAttribute(layoutAttribute.getValue(), attributeName, true);
    }

    public LayoutAttribute onConvertLayoutAttribute(Object attributeValue, String attributeName, boolean isString) {
        /*attributeName = attributeName.split(":")[1];
        String[] split = attributeName.split("_");
        attributeName = "";
        for (String refactor : split) {
            attributeName += capitalize(refactor);
        }*/
        attributeName = attributeToName(attributeName);
        return new LayoutAttribute(setter(attributeName, attributeValue, isString));
    }

    public String attributeToName(String attribute) {
        attribute = attribute.split(":")[1];
        String[] split = attribute.split("_");
        attribute = "";
        for (String refactor : split) {
            attribute += capitalize(refactor);
        }
        return attribute;
    }

    public List<LayoutAttribute> onFinish() {
        return null;
    }

    public String setter(String name, Object value, boolean isString) {
        return "set" + name + (isString ? "(\"" : "(") + value + (isString ? "\")" : ")");
    }

    public String attribute(String name, Object value) {
        return name + " = " + value;
    }

    private boolean isNumber(Object text) {
        try {
            Integer.parseInt(String.valueOf(text));
            return true;
        } catch (NumberFormatException ignore) {
            return false;
        }
    }

    private static String capitalize(String name) {
        if (name != null && name.length() != 0) {
            char[] chars = name.toCharArray();
            chars[0] = Character.toUpperCase(chars[0]);
            return new String(chars);
        } else {
            return name;
        }
    }
}

@@ -639,78 +639,33 @@
     
-LayoutAttribute layoutAttribute = new LayoutAttribute(attributeValue);
+boolean isString = true;%0A
 %0A   
@@ -671,17 +671,17 @@
       if
- 
+(
 (attribu
@@ -696,20 +696,16 @@
 sWith(%22@
-+id/
 %22) %7C%7C at
@@ -728,70 +728,128 @@
 th(%22
-@id/%22)) %7B%0A            return onConvertLayoutAttribute(%22R.id%22 +
+?%22)) && attribute.contains(%22/%22)) %7B%0A            String%5B%5D attributeSplit = attribute.split(%22/%22);%0A            String type =
  att
@@ -846,32 +846,40 @@
 type = attribute
+Split%5B0%5D
 .replace(%22@+id/%22
@@ -874,24 +874,20 @@
 lace(%22@+
-id/
 %22, %22
-.
 %22).repla
@@ -895,41 +895,124 @@
 e(%22@
-id/
 %22, %22
-.%22), attributeName,
+%22).replace(%22?%22, %22%22);%0A            attribute = %22R.%22 + type + %22.%22 + attributeSplit%5B1%5D;%0A            isString =
  false
-)
 ;%0A  
@@ -1010,37 +1010,41 @@
 false;%0A        %7D
- else
+%0A%0A       
  if (attribute.s
@@ -1054,23 +1054,24 @@
 tsWith(%22
-@
+R.
 dimen
-/
+.
 %22)) %7B%0A  
@@ -1160,24 +1160,16 @@
 mension(
-R.dimen.
 %22 + attr
@@ -1177,31 +1177,8 @@
 bute
-.replace(%22@dimen/%22, %22%22)
  + %22
@@ -1245,24 +1245,25 @@
 tsWith(%22
-@
+R.
 string
-/
+.
 %22)) %7B%0A  
@@ -1328,25 +1328,16 @@
 tString(
-R.string.
 %22 + attr
@@ -1345,32 +1345,8 @@
 bute
-.replace(%22@string/%22, %22%22)
  + %22
@@ -1676,479 +1676,8 @@
    %7D
- /*else if (attribute.startsWith(%22?attr/%22) && attributeName != null && attributeName.equals(%22Background%22)) %7B%0A            layoutAttribute = new LayoutAttribute(%22LayoutUtils.getAttrDrawable(getContext(), R.attr.%22 + attribute.replace(%22?attr/%22, %22%22) + %22)%22, false);%0A        %7D else if (attribute.startsWith(%22?attr/%22)) %7B%0A            layoutAttribute = new LayoutAttribute(%22LayoutUtils.getAttrInt(getContext(), R.attr.%22 + attribute.replace(%22?attr/%22, %22%22) + %22)%22, false);%0A        %7D */
  els
@@ -2294,23 +2294,17 @@
 ute(
-layoutA
+a
 ttribute
 .get
@@ -2303,19 +2303,8 @@
 bute
-.getValue()
 , at
@@ -2316,20 +2316,24 @@
 teName, 
-true
+isString
 );%0A    %7D
@@ -2455,245 +2455,8 @@
 ) %7B%0A
-        /*attributeName = attributeName.split(%22:%22)%5B1%5D;%0A        String%5B%5D split = attributeName.split(%22_%22);%0A        attributeName = %22%22;%0A        for (String refactor : split) %7B%0A            attributeName += capitalize(refactor);%0A        %7D*/%0A
     

use algorithm for resource to object