/**
 * Copyright (c) 2013 Cangol
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 mobi.cangol.mobile.navigation;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.widget.ActionBarContainer;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import mobi.cangol.mobile.actionbar.ActionBarActivity;
import mobi.cangol.mobile.base.BaseNavigationFragmentActivity;
import mobi.cangol.mobile.uiframe.R;

public abstract class TabNavigationFragmentActivity extends BaseNavigationFragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.setNavigationFragmentActivityDelegate(new TabNavigationFragmentActivityDelegate(
                this));
        super.onCreate(savedInstanceState);
        this.getCustomActionBar().setTitleGravity(Gravity.CENTER);
        this.getCustomActionBar().setDisplayShowHomeEnabled(false);
    }

    @Override
    abstract public void findViews();

    @Override
    abstract public void initViews(Bundle savedInstanceState);

    @Override
    abstract public void initData(Bundle savedInstanceState);

    @Override
    public boolean onSupportNavigateUp() {
        if (stack.size() <= 1) {
            stack.peek().onSupportNavigateUp();
            return true;
        } else {
            return super.onSupportNavigateUp();
        }
    }
}

class TabNavigationFragmentActivityDelegate extends
        AbstractNavigationFragmentActivityDelegate {
    private BaseNavigationFragmentActivity mActivity;
    private ViewGroup mRootView;
    private FrameLayout mMenuView;
    private FrameLayout mContentView;

    public TabNavigationFragmentActivityDelegate(
            BaseNavigationFragmentActivity activity) {
        mActivity = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mRootView = (ViewGroup) LayoutInflater.from(mActivity).inflate(
                R.layout.navigation_tab_main, null);
        mContentView = (FrameLayout) mRootView.findViewById(R.id.content_view);
        mMenuView = (FrameLayout) mRootView.findViewById(R.id.menu_view);
//        mRootView.setOnSoftKeyboardVisibilityChangeListener(new SoftKeyboardHandledLinearLayout.SoftKeyboardVisibilityChangeListener() {
//            boolean justShowMenu = false;
//
//            @Override
//            public void onSoftKeyboardShow() {
//                if (isShowMenu()) {
//                    justShowMenu = true;
//                    showMenu(false);
//                } else {
//                    justShowMenu = false;
//                }
//            }
//
//            @Override
//            public void onSoftKeyboardHide() {
//                if (justShowMenu) {
//                    new Handler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            showMenu(true);
//                        }
//                    }, 300L);
//                }
//            }
//        });
    }

    @Override
    public int getMenuFrameId() {
        return mMenuView.getId();
    }

    @Override
    public ViewGroup getRootView() {
        return mRootView;
    }

    @Override
    public ViewGroup getMenuView() {
        return mMenuView;
    }

    @Override
    public ViewGroup getContentView() {
        return mContentView;
    }

    @Override
    public void setContentView(View v) {
        mContentView.addView(v);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

    }

    @Override
    public void showMenu(boolean show) {
        if (show) {
            mMenuView.setVisibility(View.VISIBLE);
        } else {
            mMenuView.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean isShowMenu() {
        return mMenuView.getVisibility() == View.VISIBLE;
    }

    @Override
    public void setMenuEnable(boolean enable) {
        if (enable) {
            mMenuView.setVisibility(View.VISIBLE);
        } else {
            mMenuView.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public BaseNavigationFragmentActivity getActivity() {
        return mActivity;
    }

    @Override
    public void attachToActivity(Activity activity) {
        TypedArray a = activity.getTheme().obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
        int background = a.getResourceId(0, 0);
        a.recycle();

        ViewGroup contentParent = (ViewGroup) getActivity().findViewById(android.R.id.content);
        ViewGroup content = (ViewGroup) contentParent.getChildAt(0);
        contentParent.removeView(content);
        contentParent.addView(mRootView, 0);
        getContentView().addView(content);

    }

    @Override
    public void setBackgroundColor(int color) {
        mRootView.setBackgroundColor(color);
    }

    @Override
    public void setBackgroundResource(int resId) {
        mRootView.setBackgroundResource(resId);
    }

    @Override
    public FrameLayout getMaskView() {
        return null;
    }

    @Override
    public void displayMaskView(boolean show) {

    }

}

@@ -778,61 +778,8 @@
 le;%0A
-import android.support.v7.widget.ActionBarContainer;%0A
 impo
@@ -965,99 +965,8 @@
 ut;%0A
-import android.widget.LinearLayout;%0A%0Aimport mobi.cangol.mobile.actionbar.ActionBarActivity;
 %0Aimp
@@ -1155,16 +1155,330 @@
 tivity %7B
+%0A%0A    /**%0A     * %E6%AD%A4%E6%96%B9%E6%B3%95%E6%97%A0%E6%95%88 %E5%9B%BA%E5%AE%9A%E8%BF%94%E5%9B%9Efalse%0A     * @return%0A     */%0A    @Override%0A    public boolean isFloatActionBarEnabled() %7B%0A        return false;%0A    %7D%0A%0A    /**%0A     * %E6%AD%A4%E6%96%B9%E6%B3%95%E6%97%A0%E6%95%88%0A     * @param floatActionBarEnabled%0A     */%0A    @Override%0A    public void setFloatActionBarEnabled(boolean floatActionBarEnabled) %7B%0A       //%0A    %7D
 %0A    @Ov
@@ -6162,22 +6162,21 @@
 ean show) %7B%0A
-%0A
     %7D%0A%0A%7D%0A

fix bug