package com.hikapro.backpack;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.hikapro.backpack.model.AddModel;
import com.hikapro.backpack.model.DetailModel;
import com.hikapro.backpack.model.ItemModel;
import com.hikapro.backpack.model.Model;
import com.hikapro.backpack.model.PackedModel;
import com.hikapro.backpack.model.SetModel;
import com.hikapro.backpack.model.ShareModel;
import com.hikapro.backpack.model.entities.Item;
import com.hikapro.backpack.model.entities.Set;
import com.hikapro.backpack.presenter.AddPresenter;
import com.hikapro.backpack.presenter.ItemDetailPresenter;
import com.hikapro.backpack.presenter.ItemListPresenter;
import com.hikapro.backpack.presenter.PackedListPresenter;
import com.hikapro.backpack.presenter.Presenter;
import com.hikapro.backpack.presenter.SetListPresenter;
import com.hikapro.backpack.presenter.SharePresenter;
import com.hikapro.backpack.view.View;
import com.hikapro.backpack.view.fragments.AddFragment;
import com.hikapro.backpack.view.fragments.ItemDetailFragment;
import com.hikapro.backpack.view.fragments.ItemListFragment;
import com.hikapro.backpack.view.fragments.PackedListFragment;
import com.hikapro.backpack.view.fragments.SetListFragment;
import com.hikapro.backpack.view.fragments.ShareFragment;

public class MainActivity extends Activity implements View.ActivityCallback {

    private FragmentManager fragmentManager;


    private final StateMaintainer stateMaintainer =
            new StateMaintainer(getFragmentManager(), MainActivity.class.getName());

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean ret;
        switch (item.getItemId()) {
            case android.R.id.home:
                this.getFragmentManager().popBackStack();
                ret = true;
                break;
            default:
                ret = super.onOptionsItemSelected(item);
        }
        return ret;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getFragmentManager();

        stateMaintainer.init();

        if (fragmentManager.getBackStackEntryCount() == 0 && savedInstanceState == null) {
            startSetListFragment();
        } else {
            Fragment fragment = fragmentManager.findFragmentByTag(SetListFragment.class.getName());
            if (fragment != null) {
                SetListFragment     view        = (SetListFragment) fragment;
                SetListPresenter    presenter   = stateMaintainer.get(SetListPresenter.class.getName());
                SetModel            model       = stateMaintainer.get(SetModel.class.getName());

                view.setPresenter(presenter);
                presenter.setView(view);
                presenter.setModel(model);
                model.setPresenter(presenter);
            }

            fragment = fragmentManager.findFragmentByTag(ItemListFragment.class.getName());
            if (fragment != null) {
                ItemListFragment     view        = (ItemListFragment) fragment;
                ItemListPresenter    presenter   = stateMaintainer.get(ItemListPresenter.class.getName());
                ItemModel            model       = stateMaintainer.get(ItemModel.class.getName());

                view.setPresenter(presenter);
                presenter.setView(view);
                presenter.setModel(model);
                model.setPresenter(presenter);

            }

            fragment = fragmentManager.findFragmentByTag(ItemDetailFragment.class.getName());
            if (fragment != null) {
                ItemDetailFragment  view        = (ItemDetailFragment) fragment;
                ItemDetailPresenter presenter   = stateMaintainer.get(ItemDetailPresenter.class.getName());
                DetailModel         model       = stateMaintainer.get(DetailModel.class.getName());

                view.setPresenter(presenter);
                presenter.setView(view);
                presenter.setModel(model);
                model.setPresenter(presenter);
            }

            fragment = fragmentManager.findFragmentByTag(PackedListFragment.class.getName());
            if (fragment != null) {
                PackedListFragment view = (PackedListFragment) fragment;
                PackedListPresenter presenter = stateMaintainer.get(PackedListPresenter.class.getName());
                PackedModel model = stateMaintainer.get(PackedModel.class.getName());

                view.setPresenter(presenter);
                presenter.setView(view);
                presenter.setModel(model);
                model.setPresenter(presenter);
            }

            fragment = fragmentManager.findFragmentByTag(ShareFragment.class.getName());
            if (fragment != null) {
                ShareFragment       view        = (ShareFragment) fragment;
                SharePresenter      presenter   = stateMaintainer.get(SharePresenter.class.getName());
                ShareModel          model       = stateMaintainer.get(ShareModel.class.getName());

                view.setPresenter(presenter);
                presenter.setView(view);
                presenter.setModel(model);
                model.setPresenter(presenter);
            }

            fragment = fragmentManager.findFragmentByTag(AddFragment.class.getName());
            if (fragment != null) {
                AddFragment       view        = (AddFragment) fragment;
                AddPresenter      presenter   = stateMaintainer.get(AddPresenter.class.getName());
                AddModel          model       = stateMaintainer.get(AddModel.class.getName());

                view.setPresenter(presenter);
                presenter.setView(view);
                presenter.setModel(model);
                model.setPresenter(presenter);
            }
        }
        Log.i("On create", "Activity");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("onStart", "Activity");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("onRestart", "Activity");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("onResume", "Activity");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("onPause", "Activity");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("onStop", "Activity");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("onDestroy", "Activity");
    }

    @Override
    public void startSetListFragment() {

        SetListFragment     view        = SetListFragment.construct();
        SetListPresenter    presenter   = new SetListPresenter();
        SetModel            model       = new SetModel();

        view.setPresenter(presenter);
        //presenter.setView(view);
        presenter.setModel(model);
        model.setPresenter(presenter);

        replaceFragment(view, SetListFragment.class.getName(), 0);
        stateMaintainer.put(presenter);
        stateMaintainer.put(model);

    }

    @Override
    public void startItemListFragment(Set set) {

        ItemListFragment    view        = ItemListFragment.newFromSet(set);
        ItemListPresenter   presenter   = new ItemListPresenter();
        ItemModel           model       = new ItemModel();

        view.setPresenter(presenter);
        presenter.setView(view);
        presenter.setModel(model);
        model.setPresenter(presenter);

        replaceFragment(view, ItemListFragment.class.getName(),
                Presenter.ADD_TO_BACKSTACK | Presenter.TRANSITION_X);

        stateMaintainer.put(presenter);
        stateMaintainer.put(model);
    }

    @Override
    public void startPackedListFragment(Set set) {
        PackedListFragment  view        = PackedListFragment.newFromSet(set);
        PackedListPresenter presenter   = new PackedListPresenter();
        PackedModel model;

        if (stateMaintainer.hasKey(PackedModel.class.getName())) {
            model = stateMaintainer.get(PackedModel.class.getName());
        } else {
            model       = new PackedModel();
        }

        view.setPresenter(presenter);
        presenter.setView(view);
        presenter.setModel(model);
        model.setPresenter(presenter);

        replaceFragment(view, PackedListFragment.class.getName(),
                Presenter.ADD_TO_BACKSTACK | Presenter.TRANSITION_Y);

        stateMaintainer.put(presenter);
        stateMaintainer.put(model);

    }

    @Override
    public void startItemDetailFragment(int setId, Model.Item baseModel, int position) {

        ItemDetailFragment  view        = ItemDetailFragment.newInstance(setId);
        ItemDetailPresenter presenter   = new ItemDetailPresenter(baseModel, position);
        DetailModel         model       = new DetailModel();

        view.setPresenter(presenter);
        presenter.setView(view);
        presenter.setModel(model);
        model.setPresenter(presenter);

        replaceFragment(view, ItemDetailFragment.class.getName(),
                Presenter.ADD_TO_BACKSTACK | Presenter.TRANSITION_X);

        stateMaintainer.put(presenter);
        stateMaintainer.put(model);

    }

    @Override
    public void startShareFragment(int setId) {

        ShareFragment       view        = ShareFragment.construct();
        SharePresenter      presenter   = new SharePresenter();
        ShareModel          model       = new ShareModel();

        view.setPresenter(presenter);
        presenter.setView(view);
        presenter.setModel(model);
        model.setPresenter(presenter);

        replaceFragment(view, ShareFragment.class.getName(),
                Presenter.ADD_TO_BACKSTACK | Presenter.TRANSITION_X);

        stateMaintainer.put(presenter);
        stateMaintainer.put(model);
    }

    @Override
    public void startAddFragment(Set set) {
        AddFragment         view        = AddFragment.newFromSet(set);
        AddPresenter        presenter   = new AddPresenter();
        AddModel            model       = new AddModel();

        view.setPresenter(presenter);
        presenter.setView(view);
        presenter.setModel(model);
        model.setPresenter(presenter);

        replaceFragment(view, AddFragment.class.getName(),
                Presenter.ADD_TO_BACKSTACK | Presenter.TRANSITION_X);

        stateMaintainer.put(presenter);
        stateMaintainer.put(model);
    }

    private void replaceFragment(Fragment fragment, String tag, int flags) {

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if ((flags & Presenter.TRANSITION_X) == Presenter.TRANSITION_X) {
            transaction.setCustomAnimations(R.animator.slide_in_left_x, R.animator.slide_out_right_x,
                    R.animator.slide_in_right_x, R.animator.slide_out_left_x);
        }
        if ((flags & Presenter.TRANSITION_Y) == Presenter.TRANSITION_Y) {
            transaction.setCustomAnimations(R.animator.slide_in_top_y, R.animator.slide_out_bottom_y,
                    R.animator.slide_in_bottom_y, R.animator.slide_out_top_y);
        }
        transaction.replace(R.id.container, fragment, tag);

        if ((flags & Presenter.ADD_TO_BACKSTACK) == Presenter.ADD_TO_BACKSTACK) {
            transaction.addToBackStack(null);

        }
        //transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.commit();
    }
}
