/*
    ShengDao Android Client, ActivityPageManager
    Copyright (c) 2014 ShengDao Tech Company Limited
 */

package com.secspace.lib.common.utils;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.Iterator;
import java.util.Stack;

public class ActivityHelper {

    private static Stack<Activity> activityStack;
    private static ActivityHelper instance = null;
    private static final String TAG = "ActivityHelper";

    private ActivityHelper() {

    }

    public static ActivityHelper getInstance() {
        if (instance == null) {
            instance = new ActivityHelper();
        }
        return instance;
    }

    public void addActivity(Activity activity) {
        if (activityStack == null)
            activityStack = new Stack<Activity>();
        activityStack.add(activity);
    }


    public void removeActivity(Activity activity) {
        if (activityStack == null)
            return;
        if (activityStack.size() == 0)
            return;
        activityStack.remove(activity);
    }


    public void finishActivity(Class<?> cls) {
        if (activityStack == null)
            return;
        if (activityStack.size() == 0)
            return;
        Iterator<Activity> iterator = activityStack.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity.getClass().equals(cls)) {
                iterator.remove();
                activity.finish();
            }
        }
    }

    public Activity getActivity(Class<?> cls){
        if (activityStack == null)
            return null;
        if (activityStack.size() == 0)
            return null;
        Iterator<Activity> iterator = activityStack.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity.getClass().equals(cls)) {
                return activity;
            }
        }
        return null;
    }

    public Activity getActivity(){
        if (activityStack == null)
            return null;
        if (activityStack.size() == 0)
            return null;
        Iterator<Activity> iterator = activityStack.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity != null && !activity.isFinishing()) {
                return activity;
            }
        }
        return null;
    }

    public Activity getTopActivity(){
        if (activityStack == null)
            return null;
        if (activityStack.size() == 0)
            return null;
        int i = 0;
        while(i<activityStack.size()) {
            Activity activity = activityStack.elementAt(i++);
            if (activity != null && !activity.isFinishing()) {
                return activity;
            }
        }
        return null;
    }

    public void finishAllActivity() {
        try {
            if (activityStack == null)
                return;
            if (activityStack.size() == 0)
                return;
            Iterator<Activity> iterator = activityStack.iterator();
            while (iterator.hasNext()) {
                Activity activity = iterator.next();
                if (null != activity) {
                    iterator.remove();
                    activity.finish();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "finishAllActivity exception " + e.toString());
        }
    }

    /**
     * getLocalClassName()  ui.activitys.PwdChooseActivity
     * getComponentName()
     * getClass().getName() 完整类名称
     *
     * @param xClass com.skr.gallery.ui.activitys.PwdChooseActivity
     */
    public void finishAllActivityExceptXX(Class<?> xClass) {
        for (Activity act : activityStack) {
            if (xClass.getName().equals(act.getClass().getName()))
                continue;
            act.finish();
        }
    }

    public static void unbindReferences(View view) {
        try {
            if (view != null) {
                view.destroyDrawingCache();
                unbindViewReferences(view);
                if (view instanceof ViewGroup) {
                    unbindViewGroupReferences((ViewGroup) view);
                }
            }
        } catch (Throwable e) {

        }
    }

    private static void unbindViewGroupReferences(ViewGroup viewGroup) {
        int nrOfChildren = viewGroup.getChildCount();
        for (int i = 0; i < nrOfChildren; i++) {
            View view = viewGroup.getChildAt(i);
            unbindViewReferences(view);
            if (view instanceof ViewGroup)
                unbindViewGroupReferences((ViewGroup) view);
        }
        try {
            viewGroup.removeAllViews();
        } catch (Throwable mayHappen) {
            // AdapterViews, ListViews and potentially other ViewGroups don't
            // support the removeAllViews operation
        }
    }

    @SuppressWarnings("deprecation")
    private static void unbindViewReferences(View view) {
        // set all listeners to null (not every view and not every API level
        // supports the methods)
        try {
            view.setOnClickListener(null);
            view.setOnCreateContextMenuListener(null);
            view.setOnFocusChangeListener(null);
            view.setOnKeyListener(null);
            view.setOnLongClickListener(null);
            view.setOnClickListener(null);
        } catch (Throwable mayHappen) {
            //todo
        }

        // set background to null
        Drawable d = view.getBackground();
        if (d != null) {
            d.setCallback(null);
        }

        if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            d = imageView.getDrawable();
            if (d != null) {
                d.setCallback(null);
            }
            imageView.setImageDrawable(null);
            imageView.setBackgroundDrawable(null);
        }

        // destroy WebView
        if (view instanceof WebView) {
            WebView webview = (WebView) view;
            webview.stopLoading();
            webview.clearFormData();
            webview.clearDisappearingChildren();
            webview.setWebChromeClient(null);
            webview.setWebViewClient(null);
            webview.destroyDrawingCache();
            webview.destroy();
            webview = null;
        }

        if (view instanceof ListView) {
            ListView listView = (ListView) view;
            try {
                listView.removeAllViewsInLayout();
            } catch (Throwable mayHappen) {
            }
            ((ListView) view).destroyDrawingCache();
        }
    }

}
