package com.example.mitsest.myapplication.fragment.main_activity;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.mitsest.myapplication.APIGithub;
import com.example.mitsest.myapplication.MainApplication;
import com.example.mitsest.myapplication.MainActivityTransition;
import com.example.mitsest.myapplication.R;
import com.programize.mycustomviews.fragment_transition.Transition;
import com.programize.mycustomviews.datepicker.MySpinnerDatePickerDialogFragment;
import com.programize.mycustomviews.datepicker.MySpinnerDatePickerDialogFragmentMessage;
import com.programize.mydagger.callback.ProgressBarCallback;
import com.programize.mydagger.callback.ProgressDialogCallback;
import com.programize.myfunctions.MContext;
import com.programize.myfunctions.MyLog;
import com.programize.mycustomviews.retain_textviews_fragment.RetainText;
import com.programize.mycustomviews.retain_textviews_fragment.RetainTextViewsFragment;

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

import javax.inject.Inject;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by dgar on 4/6/2017.
 */

public class FragmentMain extends RetainTextViewsFragment implements View.OnClickListener {

    private MyLog log;

    @Inject
    APIGithub APIGithub;

    @RetainText
    Button zenQuoteView;

    @RetainText
    Button showDialogView_1;
    private final static String DATE_PICKER_1_ID = "DATE_PICKER_1_ID";
    private MySpinnerDatePickerDialogFragment mSpinnerDatePickerDialogFragment_1;

    @RetainText
    Button showDialogView_2;
    private final static String DATE_PICKER_2_ID = "DATE_PICKER_2_ID";
    private MySpinnerDatePickerDialogFragment mSpinnerDatePickerDialogFragment_2;


    @Override
    public void onDetach() {
        super.onDetach();

        APIGithub = null;

        zenQuoteView = null;

        showDialogView_1 = null;
        mSpinnerDatePickerDialogFragment_1 = null;

        showDialogView_2 = null;
        mSpinnerDatePickerDialogFragment_2 = null;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        log = new MyLog(MContext.getClassNameFrom(context));

        Application application = ( (Activity) context ).getApplication();
        if (! (application instanceof MainApplication) ) {
            log.e(" (AppCompatActivity) context ).getApplication() is not instanceof MainApplication");
            return;
        }

        // APIGithub inject
        ((MainApplication) application).getmComponentAPI().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, savedInstanceState);

        if (v == null)
            v = inflater.inflate(R.layout.activity_main__fragment_home, container, false);

        zenQuoteView = (Button) v.findViewById(R.id.activity_main__zen_quote_textview);
        zenQuoteView.setOnClickListener(this);

        showDialogView_1 = (Button) v.findViewById(R.id.activity_main__dialog_show_1);
        showDialogView_1.setOnClickListener(this);


        showDialogView_2 = (Button) v.findViewById(R.id.activity_main__dialog_show_2);
        showDialogView_2.setOnClickListener(this);

        Button fragmentTransitionButton =
                (Button) v.findViewById(R.id.activity_main__fragment_transition);

        fragmentTransitionButton.setOnClickListener(this);


        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_main__zen_quote_textview:
                doZenRequestWithProgressBarDialogFragment();
                break;
            case R.id.activity_main__dialog_show_1:
                doShowSpinnerDateDialogFragment_1();
                break;
            case R.id.activity_main__dialog_show_2:
                doShowSpinnerDateDialogFragment_2();
                break;
            case R.id.activity_main__fragment_transition:
                Transition transition = MainActivityTransition.getTransition(this, new Fragment1());
                transition.replace();
                break;
            default:
                break;
        }
    }

    private void doZenRequestWithProgressBarDialogFragment() {
        ProgressBarCallback<String> zenCallback = new ProgressBarCallback<String>(
                getContext(),
                getFragmentManager()
        ) {
            @Override
            public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                super.onResponse(call, response);
                zenQuoteView.setText(response.body());
            }
        };

        APIGithub.zen().enqueue(zenCallback);
    }

    private void doZenRequestWithProgressDialog() {
        ProgressDialogCallback<String> zenCallback = new ProgressDialogCallback<String>(
                getContext(),
                getString(
                        R.string.com_programize_mydagger_callback__ajax_message__please_wait
                )
        ) {
            @Override
            public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                super.onResponse(call, response);
                zenQuoteView.setText(response.body());
            }
        };

        APIGithub.zen().enqueue(zenCallback);
    }

    private void doShowSpinnerDateDialogFragment_1() {
        if (mSpinnerDatePickerDialogFragment_1 == null)
            mSpinnerDatePickerDialogFragment_1 =
                    MySpinnerDatePickerDialogFragment.newInstance(
                            DATE_PICKER_1_ID,
                            true
                    );

        mSpinnerDatePickerDialogFragment_1.show(
                getFragmentManager(),
                DATE_PICKER_1_ID
        );
    }


    private void doShowSpinnerDateDialogFragment_2() {
        if (mSpinnerDatePickerDialogFragment_2 == null)
            mSpinnerDatePickerDialogFragment_2 =
                    MySpinnerDatePickerDialogFragment.newInstance(
                            DATE_PICKER_2_ID,
                            R.layout.activity_main__datepicker,
                            true
                    );

        mSpinnerDatePickerDialogFragment_2.show(
                getFragmentManager(),
                DATE_PICKER_2_ID
        );
    }

    // -- EventBus
    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }
    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    /**
     * MySpinnerDatePickerDialogFragment communication through {@link MySpinnerDatePickerDialogFragmentMessage}
     * @param m: the message coming from the fragment on submit.
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMySpinnerDatePickerDialogFragmentSubmit(MySpinnerDatePickerDialogFragmentMessage m) {
        switch (m.id) {
            case DATE_PICKER_1_ID:
                showDialogView_1.setText(MContext.getDateFromDatePicker(m.datePicker).toString());
                break;
            case DATE_PICKER_2_ID:
                showDialogView_2.setText(MContext.getDateFromDatePicker(m.datePicker).toString());
                break;
            default:

                break;
        }
    }
    // --


}
