package de.longri.cachebox3;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
import org.robovm.apple.coregraphics.CGPoint;
import org.robovm.apple.coregraphics.CGRect;
import org.robovm.apple.coregraphics.CGSize;
import org.robovm.apple.foundation.NSRange;
import org.robovm.apple.foundation.NSString;
import org.robovm.apple.foundation.NSURL;
import org.robovm.apple.uikit.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by longri on 09.06.17.
 */
public class IOS_TextInputView extends UIView {

    private final static Logger log = LoggerFactory.getLogger(IOS_TextInputView.class);

    private CGRect previousRect = CGRect.Zero();

    public interface Callback {
        void okClicked(String text);

        void cancelClicked();
    }


    UIViewController mainViewController;
    final UIButton okButton, cancelButton;

    public IOS_TextInputView(UIViewController mainViewController, String text, final Callback callback) {
        this.mainViewController = mainViewController;
        ((IOSApplication) Gdx.app).getUIWindow().addSubview(this);


        this.setBackgroundColor(UIColor.fromRGBA(0.75, 0.75, 0.8, 1.0));

        this.okButton = new UIButton(UIButtonType.System);
        this.cancelButton = new UIButton(UIButtonType.RoundedRect);


        UIColor buttonColor = UIColor.fromRGBA(0.55, 0.55, 0.57, 1.0);

        this.okButton.setBackgroundColor(buttonColor);
        this.cancelButton.setBackgroundColor(buttonColor);

        this.okButton.setTitleColor(UIColor.black(), UIControlState.Normal);
        this.cancelButton.setTitleColor(UIColor.black(), UIControlState.Normal);


        this.okButton.setTitle("ok", UIControlState.Normal);
        this.cancelButton.setTitle("cancel", UIControlState.Normal);

        CGRect bounds = mainViewController.getView().getBounds();
        float width = (float) bounds.getWidth();
        float height = (float) (bounds.getHeight() / 1.8);
        float buttonWidth = 100;
        float buttonHeight = 50;

        CGRect rect = new CGRect(width - buttonWidth, height - buttonHeight, buttonWidth, buttonHeight);
        this.okButton.setFrame(rect);
        rect = new CGRect(0, height - buttonHeight, buttonWidth, buttonHeight);
        this.cancelButton.setFrame(rect);

        addSubview(this.okButton);
        addSubview(this.cancelButton);


        rect = new CGRect(0, 0, width, height - buttonHeight);
        final UIScrollView scrollView = new UIScrollView(rect);
        scrollView.setShowsHorizontalScrollIndicator(true);
        scrollView.setShowsVerticalScrollIndicator(true);
        scrollView.setCanCancelContentTouches(false);
        scrollView.setClipsToBounds(true);
        scrollView.setIndicatorStyle(UIScrollViewIndicatorStyle.White);
        scrollView.setScrollEnabled(true);


        rect = getSize(text);
        scrollView.setContentSize(rect.getSize());
        final UITextView textView = new UITextView(rect);
        textView.setFont(UIFont.getFont("Helvetica", 15));
        textView.setTextColor(UIColor.black());
        textView.setBackgroundColor(UIColor.white());
        textView.getLayer().setBorderColor(UIColor.lightGray().getCGColor());
        textView.getLayer().setBorderWidth(1.0);
        textView.setText(text);
        textView.setScrollEnabled(false);

        textView.setDelegate(new UITextViewDelegateAdapter() {
            @Override
            public void didChange(UITextView uiTextView) {
                CGRect currentRect = setContentSize(textView, scrollView);
                log.debug("Current Size:{}", currentRect);
                previousRect = currentRect;
            }
        });

        scrollView.addSubview(textView);
        addSubview(scrollView);

        rect = new CGRect(0, 0, width, height);
        this.setFrame(rect);

        this.okButton.addOnTouchUpInsideListener(new UIControl.OnTouchUpInsideListener() {
            @Override
            public void onTouchUpInside(UIControl uiControl, UIEvent uiEvent) {
                callback.okClicked(textView.getText());
                IOS_TextInputView.this.removeFromSuperview();
            }
        });

        this.cancelButton.addOnTouchUpInsideListener(new UIControl.OnTouchUpInsideListener() {
            @Override
            public void onTouchUpInside(UIControl uiControl, UIEvent uiEvent) {
                callback.cancelClicked();
                IOS_TextInputView.this.removeFromSuperview();
            }
        });

        Gdx.app.postRunnable(new Runnable() {
            @Override
            public void run() {
                setContentSize(textView, scrollView);
            }
        });
    }

    private CGRect setContentSize(UITextView textView, UIScrollView scrollView) {
        CGRect currentRect = getSize(textView.getText());
        if (previousRect != CGRect.Zero()) {
            if (currentRect != previousRect) {
                textView.setFrame(currentRect);
                scrollView.setContentSize(currentRect.getSize());
            }
        }
        return currentRect;
    }


    private CGRect getSize(String text) {

        NSString nsString = new NSString(text);
        UIFont uiFont = UIFont.getFont("Helvetica", 15);

        NSAttributedStringAttributes attribs = new NSAttributedStringAttributes();
        attribs.setFont(uiFont);
        CGSize bounding = nsString.getSize(attribs);

        double ext = (uiFont.getLineHeight() * 2);

        double height = Math.max(bounding.getHeight() + ext, this.getBounds().getHeight() - this.okButton.getBounds().getHeight());

        return new CGRect(0, 0, bounding.getWidth() + ext, height);
    }

}

@@ -115,54 +115,8 @@
 on;%0A
-import org.robovm.apple.coregraphics.CGPoint;%0A
 impo
@@ -318,24 +318,47 @@
 ple.
-foundation.NSURL
+gamecontroller.GCControllerDirectionPad
 ;%0Aim
@@ -450,59 +450,8 @@
 y;%0A%0A
-import java.util.concurrent.atomic.AtomicInteger;%0A%0A
 /**%0A
@@ -934,16 +934,22 @@
 troller,
+ final
  String 
@@ -4675,16 +4675,448 @@
      %7D);
+%0A%0A        Gdx.app.postRunnable(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                Gdx.app.postRunnable(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        textView.setSelectedRange(new NSRange(text.length(), 0));%0A                        textView.becomeFirstResponder();%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A
@@ -6024,25 +6024,24 @@
 ght());%0A
-%0A
         
 return n
@@ -6036,32 +6036,32 @@
     
-return new CGRect(0, 0, 
+double width = Math.max(
 boun
@@ -6082,16 +6082,85 @@
 ) + ext,
+ this.getBounds().getWidth());%0A        return new CGRect(0, 0, width,
  height)

set cursor at last position, open KeyBoard, fix width with no text!