package org.stepic.droid.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.squareup.otto.Subscribe;

import org.stepic.droid.R;
import org.stepic.droid.events.comments.NewCommentWasAdded;
import org.stepic.droid.events.steps.StepWasUpdatedEvent;
import org.stepic.droid.model.Lesson;
import org.stepic.droid.model.Step;
import org.stepic.droid.model.Unit;
import org.stepic.droid.util.AppConstants;
import org.stepic.droid.view.custom.LatexSupportableEnhancedFrameLayout;
import org.stepic.droid.web.StepResponse;

import butterknife.Bind;
import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;

public abstract class StepBaseFragment extends FragmentBase {

    @Bind(R.id.text_header_enhanced)
    protected LatexSupportableEnhancedFrameLayout headerWvEnhanced;

    @Bind(R.id.open_comments_root)
    protected View openCommentViewClickable;

    @Bind(R.id.open_comments_text)
    protected TextView textForComment;

    protected Step step;
    protected Lesson lesson;
    protected Unit unit;


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        step = (Step) getArguments().getSerializable(AppConstants.KEY_STEP_BUNDLE);
        lesson = (Lesson) getArguments().getSerializable(AppConstants.KEY_LESSON_BUNDLE);
        unit = (Unit) getArguments().getSerializable(AppConstants.KEY_UNIT_BUNDLE);

        if (step != null &&
                step.getBlock() != null &&
                step.getBlock().getText() != null &&
                !step.getBlock().getText().isEmpty()) {
            headerWvEnhanced.setText(step.getBlock().getText());
            headerWvEnhanced.setVisibility(View.VISIBLE);
        } else {
            headerWvEnhanced.setVisibility(View.GONE);
        }

        updateCommentState();

        bus.register(this);
    }

    private void updateCommentState() {
        if (step != null && step.getDiscussion_proxy() != null) {
            showComment();
        } else {
            openCommentViewClickable.setVisibility(View.GONE);
        }
    }

    private void showComment() {
        openCommentViewClickable.setVisibility(View.VISIBLE);
        openCommentViewClickable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShell.getScreenProvider().openComments(getContext(), step.getDiscussion_proxy(), step.getId());
            }
        });
        textForComment.setText(MainApplication.getAppContext().getResources().getQuantityString(R.plurals.open_comments, step.getDiscussions_count(), step.getDiscussions_count()));
    }


    @Override
    public void onResume() {
        super.onResume();
        hideSoftKeypad();
    }

    @Override
    public void onDestroyView() {
        bus.unregister(this);
        openCommentViewClickable.setOnClickListener(null);
        super.onDestroyView();
    }

    @Subscribe
    public void onNewCommentWasAdded(NewCommentWasAdded event) {
        if (step != null && event.getTargetId() == step.getId()) {
            long[] arr = new long[]{step.getId()};

            mShell.getApi().getSteps(arr).enqueue(new Callback<StepResponse>() {
                @Override
                public void onResponse(Response<StepResponse> response, Retrofit retrofit) {
                    if (response.isSuccess()) {
                        StepResponse stepResponse = response.body();
                        if (stepResponse != null && stepResponse.getSteps() != null && !stepResponse.getSteps().isEmpty()) {
                            final Step stepFromInternet = stepResponse.getSteps().get(0);
                            if (stepFromInternet != null) {
                                mThreadPoolExecutor.execute(new Runnable() {
                                    @Override
                                    public void run() {
                                        mDatabaseFacade.addStep(stepFromInternet); //fixme: fragment in closure -> leak
                                    }
                                });

                                 //fixme: it is so bad, we should be updated from model, not here =(
                                bus.post(new StepWasUpdatedEvent(stepFromInternet));
                            }
                        }
                    }
                }

                @Override
                public void onFailure(Throwable t) {

                }
            });
        }

    }

    @Subscribe
    public void onStepWasUpdated(StepWasUpdatedEvent event){
        Step eventStep = event.getStep();
        if (eventStep.getId() == step.getId()){
            step.setDiscussion_proxy(eventStep.getDiscussion_proxy()); //fixme do it in immutable way
            step.setDiscussions_count(eventStep.getDiscussions_count());
            updateCommentState();
        }
    }
}

@@ -2567,32 +2567,130 @@
   %7D%0A        %7D);%0A
+%0A        int discussionCount = step.getDiscussions_count();%0A        if (discussionCount %3E 0)%7B%0A    
         textForC
@@ -2794,25 +2794,34 @@
 mments, 
-step.getD
+discussionCount, d
 iscussio
@@ -2825,49 +2825,173 @@
 sion
-s_c
+C
 ount
-(), step.getDiscussions_count()));
+));%0A        %7D%0A        else%7B%0A            textForComment.setText(MainApplication.getAppContext().getResources().getString(R.string.open_comments_zero));%0A        %7D
 %0A   

add new title for zero comments