package jp.hazuki.yuzubrowser.toolbar.sub;

import android.content.Context;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import jp.hazuki.yuzubrowser.R;
import jp.hazuki.yuzubrowser.theme.ThemeData;
import jp.hazuki.yuzubrowser.utils.ImeUtils;
import jp.hazuki.yuzubrowser.webkit.CustomWebView;

public class WebViewFindDialogFactory {
    public static WebViewFindDialog createInstance(Context context, View layout) {
        return new WebViewFind(context, layout);
    }

    private static class WebViewFind implements WebViewFindDialog {
        private final View root;
        private final Context mContext;
        private final EditText editText;
        private final TextView match;
        private CustomWebView mCurrentWeb = null;

        public WebViewFind(Context context, View view) {
            mContext = context;
            root = view;

            editText = (EditText) root.findViewById(R.id.findEditText);
            match = (TextView) root.findViewById(R.id.howMatchTextView);
        }

        @Override
        public void show(CustomWebView web) {
            mCurrentWeb = web;
            mCurrentWeb.setFindListener(findListener);

            ImageButton buttonLeft = (ImageButton) root.findViewById(R.id.buttonLeft);
            ImageButton buttonRight = (ImageButton) root.findViewById(R.id.buttonRight);
            ImageButton buttonEnd = (ImageButton) root.findViewById(R.id.buttonEnd);

            if (ThemeData.isEnabled()) {
                ThemeData data = ThemeData.getInstance();
                root.setBackgroundColor(data.toolbarBackgroundColor);
                editText.setTextColor(data.toolbarTextColor);
                editText.setHintTextColor(data.toolbarTextColor & 0xffffff | 0x88000000);
                match.setTextColor(data.toolbarTextColor);
                buttonLeft.setColorFilter(data.toolbarImageColor);
                buttonRight.setColorFilter(data.toolbarImageColor);
                buttonEnd.setColorFilter(data.toolbarImageColor);
            }

            root.setVisibility(View.VISIBLE);
            editText.requestFocus();
            editText.postDelayed(new Runnable() {
                public void run() {
                    editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
                    editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0));
                }
            }, 100);
            editText.setText("");

            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (mCurrentWeb == null) return;

                    mCurrentWeb.clearMatches();
                    mCurrentWeb.findAllAsync(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });

            buttonLeft.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            if (mCurrentWeb == null) return;

                            ImeUtils.hideIme(mContext, editText);
                            mCurrentWeb.findNext(false);
                            mCurrentWeb.requestFocus();
                        }
                    });

            buttonRight.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            if (mCurrentWeb == null) return;

                            ImeUtils.hideIme(mContext, editText);
                            mCurrentWeb.findNext(true);
                            mCurrentWeb.requestFocus();
                        }
                    });

            buttonEnd.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            hide();
                        }
                    });
        }

        @Override
        public void hide() {
            root.setVisibility(View.GONE);
            ImeUtils.hideIme(mContext, editText);

            if (mCurrentWeb != null) {
                mCurrentWeb.clearMatches();
                mCurrentWeb.notifyFindDialogDismissedMethod();
            }
        }

        @Override
        public boolean isVisible() {
            return root.getVisibility() == View.VISIBLE;
        }

        private WebView.FindListener findListener = new WebView.FindListener() {
            @Override
            public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
                if (match != null) {
                    match.setText((numberOfMatches > 0 ? activeMatchOrdinal + 1 : 0) + "/" + numberOfMatches);
                }
            }
        };
    }
}

@@ -1,8 +1,603 @@
+/*%0A * Copyright (C) 2017 Hazuki%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 
@@ -660,24 +660,55 @@
 nt.Context;%0A
+import android.graphics.Color;%0A
 import andro
@@ -2383,24 +2383,83 @@
 Instance();%0A
+%0A                if (data.toolbarBackgroundColor != 0)%0A    
             
@@ -2508,32 +2508,175 @@
 ckgroundColor);%0A
+                else%0A                    root.setBackgroundResource(R.color.deep_gray);%0A%0A                if (data.toolbarTextColor != 0) %7B%0A    
                 
@@ -2713,32 +2713,36 @@
 lbarTextColor);%0A
+    
                 
@@ -2823,32 +2823,36 @@
                 
+    
 match.setTextCol
@@ -2870,32 +2870,299 @@
 lbarTextColor);%0A
+                %7D else %7B%0A                    editText.setTextColor(Color.WHITE);%0A                    editText.setHintTextColor(0x88ffffff);%0A                    match.setTextColor(Color.WHITE);%0A                %7D%0A%0A                if (data.toolbarImageColor != 0) %7B%0A    
                 
@@ -3220,32 +3220,36 @@
                 
+   
+ 
 buttonRight.setC
@@ -3292,32 +3292,36 @@
                 
+    
 buttonEnd.setCol
@@ -3346,32 +3346,228 @@
 barImageColor);%0A
+                %7D else %7B%0A                    buttonLeft.clearColorFilter();%0A                    buttonRight.clearColorFilter();%0A                    buttonEnd.clearColorFilter();%0A                %7D%0A
             %7D%0A%0A 

fix theme