package guard.api.sensitive.ui.sapiguard.hookedapis;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import guard.api.sensitive.ui.sapiguard.XHook;
import guard.api.sensitive.ui.sapiguard.utils.PrivacyManager;
import guard.api.sensitive.ui.sapiguard.utils.XParam;

public class XWindowManager extends XHook {
    private Methods mMethod;
    private String mClassName;
    private static final String cClassName = "android.view.WindowManagerImpl";
    private static final Map<View, WindowManager.LayoutParams> mViewParam = new WeakHashMap<View, WindowManager.LayoutParams>();
    private static int index = 0;
    private XWindowManager(Methods method, String restrictionName, String className) {
        super(restrictionName, method.name(), null);
        mMethod = method;
        mClassName = className;
    }

    public String getClassName() {
        return mClassName;
    }

    // @formatter:off

    // public void addView(View view, ViewGroup.LayoutParams params)
    // public void removeView(View view)
    // public void updateViewLayout(View view, ViewGroup.LayoutParams params)
    // http://developer.android.com/reference/android/view/ViewManager.html
    // http://developer.android.com/reference/android/view/WindowManager.html

    // @formatter:on

    private enum Methods {
        addView, removeView, updateViewLayout
    }

    ;

    public static List<XHook> getInstances(String className, boolean server) {
        List<XHook> listHook = new ArrayList<XHook>();
        if (!cClassName.equals(className)) {
            if (className == null)
                className = cClassName;

            listHook.add(new XWindowManager(Methods.addView, PrivacyManager.cOverlay, className));
            listHook.add(new XWindowManager(Methods.removeView, null, className));
            listHook.add(new XWindowManager(Methods.updateViewLayout, null, className));
        }
        return listHook;
    }

    @Override
    protected void before(XParam param) throws Throwable {
        if (mMethod == Methods.addView || mMethod == Methods.removeView || mMethod == Methods.updateViewLayout) {

        } else {
        }
    }

    @Override
    protected void after(XParam param) throws Throwable {
        Log.d("jie-XWindowManager-", "after");

        if (mMethod == Methods.addView || mMethod == Methods.removeView || mMethod == Methods.updateViewLayout) {
            View view = (View) param.args[0];
            if (view != null) {
                getScreenShot(view);
            }
        }
    }

    private void getScreenShot(View view) {
        View v1 = view;

        final String shotName = view.getClass().getName()+index;
        index+=1;
        Log.d("jie-XWindowManager", shotName);
//        Thread screenshotT = new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        });
//
//        screenshotT.start();

        String screenshotName = Environment.getExternalStorageDirectory() + File.separator +  File.separator + shotName + ".png";
        Log.d("jie-XWindowManager", screenshotName);

        File f = new File(screenshotName);
        if (f.exists())
            return;
        try {
            // create bitmap screen capture
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);
            File imageFile = new File(screenshotName);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.PNG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
