/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.taobao.weex.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import com.taobao.weex.ui.component.WXDiv;
import com.taobao.weex.ui.flat.widget.Widget;
import com.taobao.weex.ui.view.gesture.WXGesture;
import com.taobao.weex.ui.view.gesture.WXGestureObservable;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXViewUtils;
import java.lang.ref.WeakReference;
import java.util.List;

/**
 * FrameLayout wrapper
 *
 */
public class WXFrameLayout extends FrameLayout implements WXGestureObservable,IRenderStatus<WXDiv>,IRenderResult<WXDiv> {

  private WXGesture wxGesture;

  private WeakReference<WXDiv> mWeakReference;

  private List<Widget> mWidgets;

  public WXFrameLayout(Context context) {
    super(context);
  }

  @Override
  public void registerGestureListener(WXGesture wxGesture) {
    this.wxGesture = wxGesture;
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
    boolean result = super.dispatchTouchEvent(event);
    if (wxGesture != null) {
      result |= wxGesture.onTouch(this, event);
    }
    return result;
  }

  @Override
  public void holdComponent(WXDiv component) {
    mWeakReference = new WeakReference<WXDiv>(component);
  }

  @Nullable
  @Override
  public WXDiv getComponent() {
    return null != mWeakReference ? mWeakReference.get() : null;
  }

  public void mountFlatGUI(List<Widget> widgets){
    this.mWidgets = widgets;
    if (mWidgets != null) {
      setWillNotDraw(true);
    }
    invalidate();
  }

  public void unmountFlatGUI(){
    mWidgets = null;
    setWillNotDraw(false);
    invalidate();
  }

  @Override
  protected boolean verifyDrawable(@NonNull Drawable who) {
    return mWidgets != null || super.verifyDrawable(who);
  }

//  @Override
//  protected void dispatchDraw(Canvas canvas) {
//    try {
//      if (mWidgets != null) {
//        canvas.save();
//        canvas.translate(getPaddingLeft(), getPaddingTop());
//        for (Widget widget : mWidgets) {
//          widget.draw(canvas);
//        }
//        canvas.restore();
//      } else {
//        WXViewUtils.clipCanvasWithinBorderBox(this, canvas);
//        super.dispatchDraw(canvas);
//      }
//    }catch (Throwable e){
//      WXLogUtils.e("FlatGUI Crashed when dispatchDraw", WXLogUtils.getStackTrace(e));
//    }
//  }
}

@@ -2769,18 +2769,16 @@
 );%0A  %7D%0A%0A
-//
   @Overr
@@ -2781,18 +2781,16 @@
 verride%0A
-//
   protec
@@ -2824,26 +2824,24 @@
 s canvas) %7B%0A
-//
     try %7B%0A//
@@ -2838,18 +2838,16 @@
   try %7B%0A
-//
       if
@@ -2864,26 +2864,24 @@
  != null) %7B%0A
-//
         canv
@@ -2887,26 +2887,24 @@
 vas.save();%0A
-//
         canv
@@ -2948,26 +2948,24 @@
 dingTop());%0A
-//
         for 
@@ -2993,18 +2993,16 @@
 gets) %7B%0A
-//
         
@@ -3016,34 +3016,32 @@
 t.draw(canvas);%0A
-//
         %7D%0A//    
@@ -3030,26 +3030,24 @@
 ;%0A        %7D%0A
-//
         canv
@@ -3060,18 +3060,16 @@
 tore();%0A
-//
       %7D 
@@ -3075,18 +3075,16 @@
  else %7B%0A
-//
         
@@ -3132,26 +3132,24 @@
 s, canvas);%0A
-//
         supe
@@ -3176,18 +3176,16 @@
 s);%0A
-//
       %7D%0A
 //  
@@ -3180,18 +3180,16 @@
       %7D%0A
-//
     %7Dcat
@@ -3206,18 +3206,16 @@
 ble e)%7B%0A
-//
       WX
@@ -3296,18 +3296,14 @@
 ));%0A
-//
     %7D%0A
-//
   %7D%0A

Revert "* [Core] Turn off FlatGUI."