/*
 * Copyright 2017, Flávio Keglevich
 *
 * 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.fkeglevich.rawdumper.controller.feature;

import android.os.Handler;
import android.os.Looper;
import android.view.MotionEvent;
import android.view.View;

import com.fkeglevich.rawdumper.camera.action.listener.AutoFocusResult;
import com.fkeglevich.rawdumper.camera.async.TurboCamera;
import com.fkeglevich.rawdumper.camera.data.PreviewArea;
import com.fkeglevich.rawdumper.camera.feature.FocusFeature;
import com.fkeglevich.rawdumper.ui.TouchFocusView;
import com.fkeglevich.rawdumper.ui.UiUtil;

/**
 * TODO: Add class header
 * <p>
 * Created by Flávio Keglevich on 28/10/17.
 */

public class TouchFocusController extends FeatureController
{
    private final TouchFocusView focusView;
    private final Handler uiHandler;
    private FocusFeature focusFeature;
    private boolean enabled = true;
    private PreviewArea lastTouchArea = null;

    private final AutoFocusResult autoFocusResult = new AutoFocusResult()
    {
        @Override
        public void autoFocusDone(final boolean success)
        {
            uiHandler.post(new Runnable()
            {
                @Override
                public void run()
                {
                    focusView.setMeteringArea(lastTouchArea,
                            success ? TouchFocusView.FOCUS_METERING_SUCCESS : TouchFocusView.FOCUS_METERING_FAIL);

                };
            });
        }
    };

    TouchFocusController(View clickArea, TouchFocusView focusView)
    {
        this.focusView = focusView;
        this.uiHandler = new Handler(Looper.getMainLooper());
        this.focusFeature = null;

        clickArea.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if(enabled && event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    if (focusFeature != null && focusFeature.getValue().canAutoFocus())
                    {
                        int touchSize = UiUtil.dpToPixels(36, TouchFocusController.this.focusView.getContext());
                        lastTouchArea = PreviewArea.createTouchArea(v, event, touchSize);
                        TouchFocusController.this.focusView.setMeteringArea(lastTouchArea, TouchFocusView.FOCUS_METERING);
                        focusFeature.startAutoFocus(lastTouchArea, autoFocusResult);
                    }

                    v.performClick();
                }
                return false;
            }
        });
    }

    @Override
    protected void setup(TurboCamera camera)
    {
        focusFeature = camera.getFocusFeature();
        if (!focusFeature.isAvailable())
            reset();
    }

    @Override
    protected void reset()
    {
        focusFeature = null;
        focusView.setMeteringArea(null, TouchFocusView.FOCUS_METERING);
    }

    @Override
    protected void disable()
    {
        enabled = false;
    }

    @Override
    protected void enable()
    {
        enabled = true;
    }
}

@@ -892,16 +892,71 @@
 Camera;%0A
+import com.fkeglevich.rawdumper.camera.data.FocusMode;%0A
 import c
@@ -1065,16 +1065,87 @@
 eature;%0A
+import com.fkeglevich.rawdumper.camera.parameter.ParameterChangeEvent;%0A
 import c
@@ -1229,16 +1229,74 @@
 .UiUtil;
+%0Aimport com.fkeglevich.rawdumper.util.event.EventListener;
 %0A%0A/**%0A *
@@ -3455,24 +3455,34 @@
 vailable())%0A
+        %7B%0A
             
@@ -3486,24 +3486,399 @@
    reset();%0A
+            return;%0A        %7D%0A%0A        focusFeature.getOnChanged().addListener(new EventListener%3CParameterChangeEvent%3CFocusMode%3E%3E()%0A        %7B%0A            @Override%0A            public void onEvent(ParameterChangeEvent%3CFocusMode%3E eventData)%0A            %7B%0A                if (!eventData.parameterValue.canAutoFocus())%0A                    cleanFocus();%0A            %7D%0A        %7D);%0A
     %7D%0A%0A    @
@@ -3960,69 +3960,19 @@
     
-focusView.setMeteringArea(null, TouchFocusView.FOCUS_METERING
+cleanFocus(
 );%0A 
@@ -4098,32 +4098,32 @@
  enable()%0A    %7B%0A
-
         enabled 
@@ -4135,11 +4135,126 @@
 e;%0A    %7D
+%0A%0A    private void cleanFocus()%0A    %7B%0A        focusView.setMeteringArea(null, TouchFocusView.FOCUS_METERING);%0A    %7D
 %0A%7D%0A

Prepare TouchFocusController to react properly when focus modes are used