package com.usai.apex.mainframe;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.usai.apex.ApexTrackingApplication;
import com.usai.apex.R;
import com.usai.apex.SegmentView;

//import android.app.FragmentTransaction;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements KPIFragment.KPIPagerScrollListener{

    private SegmentView mSegmentView;
//    boolean bdirty = false;
    private  Fragment activeFragment;

//    private
    private KPIFragment kpiFragment;
    private RecentFragment recentFragment;
    public HomeFragment() {

        // Required empty public constructor
    }
    public void checkDirty()
    {
//        if(bdirty)
//            requestData();

        if(ApexTrackingApplication.get_instance().recentactive)
        {
            recentFragment.checkDirty();
        }
        else
        {
            kpiFragment.checkDirty();
        }
    }
    private int mode = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_home, container, false);




        if(kpiFragment==null) {
            kpiFragment = new KPIFragment();

//                    .add(R.id.container, kpiFragment).hide(kpiFragment).commit();
        }

        if(recentFragment==null) {
            recentFragment = new RecentFragment();
//            getChildFragmentManager().beginTransaction()
//                    .add(R.id.container, recentFragment).commit();
        }

        showFragment(mode);

//        if(mode==0)
//            ft.replace(R.id.container,kpiFragment);
//        else
//            ft.replace(R.id.container,kpiFragment);
//
//        if(mode==0)
//        {
//
//            getChildFragmentManager().beginTransaction().show(getChildFragmentManager().getFragments().get(1)).hide(getChildFragmentManager().getFragments().get(0)).commit();
//
////            getChildFragmentManager().beginTransaction().show(recentFragment).hide(kpiFragment).commit();
//        }
//        else if(mode==1)
//        {
//            getChildFragmentManager().beginTransaction().show(getChildFragmentManager().getFragments().get(0)).hide(getChildFragmentManager().getFragments().get(1)).commit();
//
////            getChildFragmentManager().beginTransaction().show(kpiFragment).hide(recentFragment).commit();
//        }


        mSegmentView = v.findViewById(R.id.segmentview);

        mSegmentView.setSegmentText("Recent",0);
        mSegmentView.setSegmentText("KPI",1);

                mSegmentView.setOnSegmentViewClickListener(new SegmentView.onSegmentViewClickListener() {
            @Override
            public void onSegmentViewClick(View view, int postion) {
                switch (postion) {
                    case 0:

                        mode = 0;
                        ((RootActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);


//                        ((Activity)getActivity()).setTitle(null);
//                        getChildFragmentManager().beginTransaction().show(recentFragment).hide(kpiFragment).commit();

//                        getChildFragmentManager().beginTransaction().show(getChildFragmentManager().getFragments().get(1)).hide(getChildFragmentManager().getFragments().get(0)).commit();



                        break;
                    case 1:

                        mode = 1;
                        ((RootActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(true);

//                        ((Activity)getActivity()).setTitle(null);


//                        getChildFragmentManager().beginTransaction().show(getChildFragmentManager().getFragments().get(0)).hide(getChildFragmentManager().getFragments().get(1)).commit();

//                        getChildFragmentManager().beginTransaction().show(kpiFragment).hide(recentFragment).commit();

                        break;
                    default:
                        break;
                }
                showFragment(mode);
            }
        });
        return v;
    }


    void showFragment(int mode)
    {

        if(mode==0)
            ApexTrackingApplication.getInstance().recentactive = true;
        else
            ApexTrackingApplication.getInstance().recentactive = false;
        FragmentManager fm=getChildFragmentManager();
        FragmentTransaction ft= fm .beginTransaction();
        if(activeFragment!=null)
            ft.hide(activeFragment);
        Fragment fragment = null;
        String tag = "";
        if(mode==0) {
            tag = "recent";
            fragment = fm.findFragmentByTag(tag);
            if(fragment==null)
                fragment = recentFragment;
            else
                recentFragment = (RecentFragment) fragment;

        }
        else
        {
            tag = "kpi";
            fragment = fm.findFragmentByTag(tag);
            if(fragment==null)
                fragment = kpiFragment;
            else
                kpiFragment = (KPIFragment) fragment;

            kpiFragment.setKPIScrollListener(this);
        }
        activeFragment = fragment;

        Log.d("Home Switch Mode", "UserHint: " + getUserVisibleHint() + " isHidden: " + isHidden());
        if (activeFragment instanceof KPIFragment) {
            kpiFragment.showTitle(getActivity(),true);
        } else {
            kpiFragment.showTitle(getActivity(),false);
        }

        if (!fragment.isAdded()) {
            ft.add(R.id.container, fragment, tag);
        } else {

            ft.show(fragment);
        }
        ft.commit();


        if(ApexTrackingApplication.get_instance().recentactive)
        {
            recentFragment.checkDirty();
        }
        else
        {
            kpiFragment.checkDirty();
        }
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("mode",mode);

    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);

            if(savedInstanceState!=null) {
                mode = savedInstanceState.getInt("mode");
                mSegmentView.setSelect(mode);
            }
    }

    @Override
    public void KPIPagerDidScrollToPage(int position) {
        Log.d("Home Switch Mode", " Scroll UserHint: "  + getUserVisibleHint() + " isHidden: " + isHidden());
        if (activeFragment instanceof KPIFragment) {
            kpiFragment.showTitle(getActivity(),true);
        } else {
            kpiFragment.showTitle(getActivity(),false);
        }
    }
}
