package ba.agraham.codeeditorview;

import com.amrdeveloper.codeview.CodeView;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DesignerName;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA.Property;
import anywheresoftware.b4a.BA.DesignerProperties;


import anywheresoftware.b4a.objects.CustomViewWrapper;
import anywheresoftware.b4a.objects.EditTextWrapper;
import anywheresoftware.b4a.objects.PanelWrapper;
import anywheresoftware.b4a.objects.LabelWrapper;
import anywheresoftware.b4a.objects.drawable.CanvasWrapper;
import anywheresoftware.b4a.objects.drawable.BitmapDrawable;
import anywheresoftware.b4a.keywords.Common.DesignerCustomView;
import anywheresoftware.b4a.objects.collections.Map;

import java.util.regex.Pattern;



@BA.ActivityObject
@BA.ShortName("CodeEditorView")
@DesignerProperties(values = { } )		
@Author("Andrew Graham")
@Version(1.0f)

public class CodeViewWrapper extends EditTextWrapper implements DesignerCustomView
{
	@Override
	public void DesignerCreateView(PanelWrapper base, LabelWrapper lw, Map props) {
 		CodeView cv = (CodeView)getObject();
		CustomViewWrapper.replaceBaseWithView(base, cv);
 	}
	
	@Hide
	@Override
	public void _initialize(final BA ba, Object activityClass, String EventName) {
		final String eventName = EventName.toLowerCase(BA.cul);
		final CodeView cv = new CodeView(ba.context);
		setObject(cv);
		innerInitialize(ba, eventName, true);
	}
	
	// 1.00	Initial attempt
	
	/**
	 *This is a custom text editor view for editing program code
   *It is based on a MultiAutoCompleteTextView which an extension of EditText.
   *So many of the EditText properties and methods are also available.
   *It uses regex pattern matching to implement syntax highlighting for the edited code
   *The patterns and colours are added using AddPattern(pattern, color, caseinsensitive)
   *   
	 *You must add the line '#AdditionalJar:CodeView' to your Main module attributes
   *
	 *This library uses code from 
	 *https://github.com/AmrDeveloper/CodeView
	 *
   *Use of this code is permitted under the MIT License
   *
   *Copyright (c) 2020 Amr Hesham
   *Copyright (c) 2020 Andre Graham
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   *of this software and associated documentation files (the "Software"), to deal
   *in the Software without restriction, including without limitation the rights
   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   *copies of the Software, and to permit persons to whom the Software is
   *furnished to do so, subject to the following conditions:
   *
   *The above copyright notice and this permission notice shall be included in all
   *copies or substantial portions of the Software.
   *
   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   *THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   *SOFTWARE.
 */	
	public void LIBRARY_DOC()
	{
	}	
	
   private String draweventname;
   private BA savedba;
   
	@BA.Hide
	public void innerInitialize(BA ba, String eventName, boolean keepOldObject)
	{
		draweventname = eventName ;   
		savedba = ba;
		if (!keepOldObject) {
			setObject(new CodeView(ba.context));
		}
		super.innerInitialize(ba, eventName, true);
	}

	
	/**
	 *Add a regex pattern and a colour
   *Matches to the regex pattern in the code will be highlighted in the specified color.   
	 */
	public void AddPattern(String pattern, int color, boolean caseinsensitive)
	{
    Pattern p;
    if (caseinsensitive)      
      p =  Pattern.compile(pattern, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); 
    else
      p =  Pattern.compile(pattern, Pattern.MULTILINE);    
		((CodeView)getObject()).addSyntaxPattern(p, color);
	}
  
	/**
	 *Rehighlight the text depending on any new patterns
   *Normally syntax highlighting is updated whenever the text changes.
	 */
	public void ReHighlightSyntax()
	{
		((CodeView)getObject()).reHighlightSyntax();
	}
  
 	/**
	 *Add an error line indication with color to support errors, hints, warnings...etc.
	 */
	public void AddErrorLine(int lineNumber, int color)
	{
		((CodeView)getObject()).addErrorLine(lineNumber, color);
	}
  
 	/**
	 *Remove an error line.
	 */
	public void RemoveErrorLine(int lineNumber)
	{
		((CodeView)getObject()).removeErrorLine(lineNumber);
	}
  
 	/**
	 *Clear all error lines.
	 */
	public void RemoveAllErrorLines()
	{
		((CodeView)getObject()).removeAllErrorLines();
	}
  
 	/**
	 *Rehighlight the error lines.
	 */
	public void ReHighlightErrors()
	{
		((CodeView)getObject()).reHighlightErrors();
	}
  
 
 	/**
	 *Set highlighter update frequency. The default is 500mS.
   *More powerful devices can probably do it more often without noticeable lagging. 
	 */
	public void SetUpdateDelayTime(int millisecs)
	{
		((CodeView)getObject()).setUpdateDelayTime(millisecs);
	} 
  
  
  
}  
  
