/*
 * Copyright (c) 2016, Amanj Sherwany <http://www.amanj.me>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list
 *     of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *     of conditions and the following disclaimer in the documentation and/or other
 *     materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be
 *     used to endorse or promote products derived from this software without specific prior
 *     written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package me.amanj.splittimer.ui;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import me.amanj.splittimer.R;
import me.amanj.splittimer.messages.Message;
import me.amanj.splittimer.messages.MessageTag;
import me.amanj.splittimer.messages.Send;
import me.amanj.splittimer.util.TimeInformation;

/**
 * Created by Amanj Sherwany on 8/14/16.
 */
public class ShowHistoryFragment extends Fragment {
    EventBus bus = EventBus.getDefault();

    private SplitTimerListFragment splitTimerListFragment;
    private ShowTimeInformationFragment showTimeInformationFragment;
    private View rootView;
    private FragmentManager mFragmentManager;
    private FrameLayout mItemsFrameLayout, mDetailFrameLayout;
    private static final int MATCH_PARENT = LinearLayout.LayoutParams.MATCH_PARENT;


    @Override
    public void onResume() {
        super.onResume();
        bus.register(this);
    }

    @Override
    public void onPause() {
        bus.unregister(this);
        super.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
//        outState.putBoolean("recreateFragmentDetail",
//                inTwoPanesMode() && showTimeInformationFragment.isAdded());
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onActivityCreated(savedInstanceState);
//        if(savedInstanceState != null &&
//                savedInstanceState.getBoolean("recreateFragmentDetail"))
//            getFragmentManager().beginTransaction().add(R.id.detail_frag,
//                    showTimeInformationFragment);
    }

    public static ShowHistoryFragment newInstance() {
        ShowHistoryFragment fragment = new ShowHistoryFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        rootView = inflater.inflate(R.layout.fragment_show_history, container, false);

        mFragmentManager = getActivity().getSupportFragmentManager();

        splitTimerListFragment = (SplitTimerListFragment)
                mFragmentManager.findFragmentByTag(SplitTimerListFragment.TAG);
        showTimeInformationFragment = (ShowTimeInformationFragment)
                mFragmentManager.findFragmentByTag(ShowTimeInformationFragment.TAG);

        if(splitTimerListFragment == null) {
            splitTimerListFragment = SplitTimerListFragment.newInstance();
        }
        if(showTimeInformationFragment == null) {
            showTimeInformationFragment = ShowTimeInformationFragment.newInstance();
        }

        boolean inTwoPanesMode = inTwoPanesMode();

        if(inTwoPanesMode) {
            mItemsFrameLayout = (FrameLayout) rootView.findViewById(R.id.list_frag);
            mDetailFrameLayout = (FrameLayout) rootView.findViewById(R.id.detail_frag);

            // Add a OnBackStackChangedListener to reset the layout when the back stack changes
            mFragmentManager.addOnBackStackChangedListener(
                new FragmentManager.OnBackStackChangedListener() {
                    public void onBackStackChanged() {
                        setLayout();
                    }
                });
        }

        if(savedInstanceState != null) return rootView;

        if(!inTwoPanesMode) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            transaction.add(R.id.fragment_container,
                    splitTimerListFragment, SplitTimerListFragment.TAG);
            transaction.commit();
        } else {

            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.list_frag, splitTimerListFragment,
                    SplitTimerListFragment.TAG);
            fragmentTransaction.commit();

        }


        return rootView;
    }


    @Subscribe
    public void onEvent(Message event) {
        if (event.tag() == MessageTag.OPEN) {
            TimeInformation lastOpened = ((Send<TimeInformation>) event).receive();

            if (showTimeInformationFragment == null)
                showTimeInformationFragment = ShowTimeInformationFragment.newInstance();

            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            if (!inTwoPanesMode()) {
                transaction.replace(R.id.fragment_container,
                        showTimeInformationFragment, ShowTimeInformationFragment.TAG);
            } else if (!showTimeInformationFragment.isAdded()) {

                transaction.add(R.id.detail_frag,
                        showTimeInformationFragment, ShowTimeInformationFragment.TAG);
            }
            transaction.addToBackStack(null);
            transaction.commit();
            mFragmentManager.executePendingTransactions();
            openTimeInformation(lastOpened);
        } else if (event.tag() == MessageTag.REMOVE_DETAILED_PANE) {
            if(inTwoPanesMode()) {
                mItemsFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(
                        MATCH_PARENT, MATCH_PARENT));
                mDetailFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
                        MATCH_PARENT));
            }
        }
    }


    private void setLayout() {
        if (!showTimeInformationFragment.isAdded()) {
            mItemsFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(
                    MATCH_PARENT, MATCH_PARENT));
            mDetailFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
                    MATCH_PARENT));
        } else {
            mItemsFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
                    MATCH_PARENT, 1f));
            mDetailFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
                    MATCH_PARENT, 2f));
        }

    }

    public void openTimeInformation(final TimeInformation tinfo) {
        bus.post(new Send<TimeInformation>() {
            public MessageTag tag() { return MessageTag.SHOW; }
            public TimeInformation receive() {
                return tinfo;
            }
        });
    }

    private boolean inTwoPanesMode() {
        return rootView.findViewById(R.id.fragment_container) == null;
    }


}

@@ -5397,16 +5397,55 @@
 != null)
+ %7B%0A            setLayout();%0A           
  return 
@@ -5453,16 +5453,26 @@
 ootView;
+%0A        %7D
 %0A%0A      

Make the detailed fragment persist upon orientation change