package com.anren.omplayer;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.facebook.react.uimanager.ThemedReactContext;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lcg on 2017/6/23.
 */

public class OMFullScreenManager {

    private final String FULLSCREEN_TAG = "fullscreen";

    private LinearLayout fullScreenView = null;
    private ViewGroup rootView = null;
    private Activity activity = null;
    private Map<Integer, Integer> viewVisibility = new HashMap<Integer, Integer>();

    public OMFullScreenManager(ThemedReactContext themedReactContext){
        activity = themedReactContext.getCurrentActivity();
        LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        fullScreenView = (LinearLayout) layoutInflater.inflate(R.layout.fullscreen, null);
        fullScreenView.setTag(FULLSCREEN_TAG);
        rootView = (ViewGroup)((ViewGroup) (activity.getWindow().getDecorView()));
    }

    @Override
    protected void finalize() throws Throwable {
        if (rootView != null && fullScreenView != null){
            rootView.removeView(fullScreenView);
        }

        super.finalize();
    }

    public void addView(View view){
        fullScreenView.addView(view);
    }

    public void removeView(View view){
        fullScreenView.removeView(view);
    }

    public void show(){
//        View viewByTagFullscreen = rootView.findViewWithTag(FULLSCREEN_TAG);

//        if(viewByTagFullscreen == null){
//            fullScreenView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
//            rootView.addView(fullScreenView);
//        }
//        else if (viewByTagFullscreen == fullScreenView) {
//            fullScreenView.setVisibility(View.VISIBLE);
//        }
//        else {
//
//        }

        fullScreenView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        rootView.addView(fullScreenView);

        hideSiblings();
        toggleHideyBar();
    }

    public void hide(){
        if (fullScreenView != null){
            rootView.removeView(fullScreenView);
        }

        restoreSiblings();
        toggleHideyBar();
    }

    private void hideSiblings() {
        int count = rootView.getChildCount();

        for (int i = 0; i < count; i++) {
            View siblingView = rootView.getChildAt(i);

            if (FULLSCREEN_TAG.equals(siblingView.getTag())){
                continue;
            }

            viewVisibility.put(i,siblingView.getVisibility());

            siblingView.setVisibility(View.INVISIBLE);
        }
    }

    private void restoreSiblings() {
        int count = rootView.getChildCount();

        for (int i = 0; i < count; i++) {
            View siblingView = rootView.getChildAt(i);

            if (FULLSCREEN_TAG.equals(siblingView.getTag())){
                continue;
            }

            Integer visibility = viewVisibility.get(i);
            if (visibility != null) {
                siblingView.setVisibility(visibility);
            }
        }
    }

    public void toggleHideyBar() {

        // BEGIN_INCLUDE (get_current_ui_flags)
        // The UI options currently enabled are represented by a bitfield.
        // getSystemUiVisibility() gives us that bitfield.
        int uiOptions = activity.getWindow().getDecorView().getSystemUiVisibility();
        int newUiOptions = uiOptions;
        // END_INCLUDE (get_current_ui_flags)
        // BEGIN_INCLUDE (toggle_ui_flags)
        boolean isImmersiveModeEnabled =
                ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);

        // Navigation bar hiding:  Backwards compatible to ICS.
        if (Build.VERSION.SDK_INT >= 14) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }

        // Status bar hiding: Backwards compatible to Jellybean
        if (Build.VERSION.SDK_INT >= 16) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
        }

        // Immersive mode: Backward compatible to KitKat.
        // Note that this flag doesn't do anything by itself, it only augments the behavior
        // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
        // all three flags are being toggled together.
        // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
        // Sticky immersive mode differs in that it makes the navigation and status bars
        // semi-transparent, and the UI flag does not get cleared when the user interacts with
        // the screen.
        if (Build.VERSION.SDK_INT >= 18) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }

        activity.getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
        //END_INCLUDE (set_ui_flags)
    }
}
