/*
 * Copyright 2011 Jon S Akhtar (Sylvanaar)
 *
 *   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 com.sylvanaar.idea.Lua.debugger;

import com.intellij.util.ObjectUtils;
import com.intellij.xdebugger.frame.XValue;
import com.intellij.xdebugger.frame.XValueNode;
import com.intellij.xdebugger.frame.XValuePlace;
import com.intellij.xdebugger.frame.presentation.XNumericValuePresentation;
import com.intellij.xdebugger.frame.presentation.XRegularValuePresentation;
import com.intellij.xdebugger.frame.presentation.XStringValuePresentation;
import com.intellij.xdebugger.frame.presentation.XValuePresentation;
import org.jetbrains.annotations.NotNull;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaValue;

import javax.swing.*;

/**
 * Created by IntelliJ IDEA.
 * User: Jon S Akhtar
 * Date: 5/15/11
 * Time: 5:34 AM
 */
public class LuaDebugValue extends XValue {
    private String myValueAsString;
    private String myTypeName;
    private final LuaValue myRawValue;
    private final Icon myIcon;

    LuaDebugValue(String typeName, String stringValue, Icon icon) {
        myValueAsString = stringValue;
        myTypeName = typeName;
        myRawValue = null;
        myIcon = icon;
    }

    LuaDebugValue(LuaValue rawValue, Icon icon) {
        myRawValue = rawValue;
        myTypeName = rawValue.typename();
        myIcon = icon;
        if (myTypeName.equals("function")) {
            final LuaValue value = myRawValue.checkclosure().call();
            final LuaString luaString = value.checkstring();

            myValueAsString = luaString.toString();
        } else if (myTypeName.equals("table")) {
            final LuaValue luaValue = myRawValue.checktable().get(LuaString.valueOf("_____ID"));
            final LuaString luaString = luaValue.checkstring();

            myValueAsString = luaString.toString();
        } else {
            myValueAsString = rawValue.toString();

        }
    }

    public boolean isString() {
        return myTypeName.equals("string");
    }

    public boolean isNumber() {
        return myTypeName.equals("number");
    }

    public boolean isBool() {
        return myTypeName.equals("boolean");
    }

    public boolean isTable() {
        return myTypeName.equals("table");
    }

    @Override
    public void computePresentation(@NotNull XValueNode node, @NotNull XValuePlace place) {
        XValuePresentation presentation = getPresentation();
        node.setPresentation(myIcon, presentation, isTable());
    }

    @NotNull
    private XValuePresentation getPresentation() {
        final String stringValue = myValueAsString;
        if (isNumber()) return new XNumericValuePresentation(stringValue);
        if (isString()) return new XStringValuePresentation(stringValue);
        if (isBool()) {
            return new XValuePresentation() {
                @Override
                public void renderValue(@NotNull XValueTextRenderer renderer) {
                    renderer.renderValue(stringValue);
                }
            };
        }
        return new XRegularValuePresentation(stringValue, myTypeName);
    }

    LuaValue getRawValue() {
        return ObjectUtils.notNull(myRawValue, LuaValue.NONE);
    }

}

@@ -715,16 +715,73 @@
 Utils;%0D%0A
+import com.intellij.xdebugger.frame.XReferrersProvider;%0D%0A
 import c
@@ -1259,16 +1259,60 @@
 tNull;%0D%0A
+import org.jetbrains.annotations.Nullable;%0D%0A
 import o
@@ -1390,16 +1390,42 @@
 wing.*;%0D
+%0Aimport java.util.Arrays;%0D
 %0A%0D%0A/**%0D%0A
@@ -1703,16 +1703,57 @@
 myIcon;%0D
+%0A    private final String identityValue;%0D
 %0A%0D%0A    L
@@ -1936,24 +1936,55 @@
 on = icon;%0D%0A
+        identityValue = null;%0D%0A
     %7D%0D%0A%0D%0A   
@@ -2172,24 +2172,188 @@
 ction%22)) %7B%0D%0A
+            if (myRawValue.isfunction()) %7B%0D%0A                myValueAsString = %22function%22;%0D%0A                identityValue = null; // todo%0D%0A            %7D else %7B%0D%0A    
             
@@ -2406,24 +2406,28 @@
 ().call();%0D%0A
+    
             
@@ -2486,39 +2486,41 @@
             
-myValueAsString
+    identityValue
  = luaString
@@ -2514,33 +2514,34 @@
 e = luaString.to
-S
+js
 tring();%0D%0A      
@@ -2526,32 +2526,47 @@
 g.tojstring();%0D%0A
+            %7D%0D%0A
         %7D else i
@@ -2776,31 +2776,29 @@
         
-myValueAsString
+identityValue
  = luaSt
@@ -2804,17 +2804,18 @@
 tring.to
-S
+js
 tring();
@@ -2878,32 +2878,65 @@
 ue.toString();%0D%0A
+            identityValue = null;
 %0D%0A        %7D%0D%0A   
@@ -4126,27 +4126,104 @@
 ntation(
-stringValue
+ObjectUtils.notNull(ObjectUtils.coalesce(Arrays.asList(stringValue, identityValue, %22%22)))
 , myType

Fix an issue with functions - will need to fix it later