<commit_before>/*
 * Copyright (c) 2015 SatuSatuDua.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package id.satusatudua.sigap.ui.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import id.satusatudua.sigap.R;
import id.satusatudua.sigap.data.model.CandidateHelper;
import id.satusatudua.sigap.data.model.Case;
import id.satusatudua.sigap.data.model.Message;
import id.satusatudua.sigap.data.model.User;
import id.satusatudua.sigap.presenter.ChatPresenter;
import id.satusatudua.sigap.ui.HelpingActivity;
import id.satusatudua.sigap.ui.MainActivity;
import id.satusatudua.sigap.ui.adapter.ChatAdapter;
import id.satusatudua.sigap.ui.adapter.HelperAdapter;
import id.zelory.benih.ui.fragment.BenihFragment;
import id.zelory.benih.ui.view.BenihRecyclerView;
import timber.log.Timber;

/**
 * Created on : January 13, 2016
 * Author     : zetbaitsu
 * Name       : Zetra
 * Email      : <EMAIL>
 * GitHub     : https://github.com/zetbaitsu
 * LinkedIn   : https://id.linkedin.com/in/zetbaitsu
 */
public class ChatFragment extends BenihFragment implements ChatPresenter.View {
    private static final String KEY_CASE = "extra_case";
    private static final String KEY_REPORTER = "extra_reporter";
    private static final String KEY_DISABLE_CHAT = "extra_disable_chat";


    @Bind(R.id.list_message) BenihRecyclerView listMessage;
    @Bind(R.id.list_helper) BenihRecyclerView listHelper;
    @Bind(R.id.button_helpers) ImageView buttonHelpers;
    @Bind(R.id.divider) View divider;
    @Bind(R.id.field_message) EditText messageField;
    @Bind(R.id.button_send) ImageView buttonSend;
    @Bind(R.id.root_input) LinearLayout rootInput;

    private ChatAdapter chatAdapter;
    private HelperAdapter helperAdapter;
    private Case theCase;
    private User reporter;
    private ChatPresenter chatPresenter;
    private ProgressDialog progressDialog;

    public static ChatFragment newInstance(Case theCase, User reporter) {
        ChatFragment chatFragment = new ChatFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_CASE, theCase);
        bundle.putParcelable(KEY_REPORTER, reporter);
        chatFragment.setArguments(bundle);
        return chatFragment;
    }

    public static ChatFragment newInstance(Case theCase, User reporter, boolean disableChat) {
        ChatFragment chatFragment = new ChatFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_CASE, theCase);
        bundle.putParcelable(KEY_REPORTER, reporter);
        bundle.putBoolean(KEY_DISABLE_CHAT, disableChat);
        chatFragment.setArguments(bundle);
        return chatFragment;
    }

    @Override
    protected int getResourceLayout() {
        return R.layout.fragment_chat;
    }

    @Override
    protected void onViewReady(@Nullable Bundle savedInstanceState) {
        resolveTheCase(savedInstanceState);
        resolveReporter(savedInstanceState);

        boolean disableChat = getArguments().getBoolean(KEY_DISABLE_CHAT, false);
        rootInput.setVisibility(disableChat ? View.GONE : View.VISIBLE);
        listHelper.setVisibility(disableChat ? View.GONE : View.VISIBLE);

        chatAdapter = new ChatAdapter(getActivity());
        listMessage.setUpAsBottomList();
        listMessage.setAdapter(chatAdapter);

        helperAdapter = new HelperAdapter(getActivity());
        listHelper.setUpAsHorizontalList();
        helperAdapter.add(transformReporter());
        listHelper.setAdapter(helperAdapter);
        helperAdapter.setOnItemClickListener((view, position) -> onItemHelperClicked(helperAdapter.getData().get(position)));

        chatPresenter = new ChatPresenter(this, theCase);
        if (savedInstanceState == null) {
            chatPresenter.loadHelper();
        } else {
            chatPresenter.loadState(savedInstanceState);
        }
    }

    private CandidateHelper transformReporter() {
        CandidateHelper reporter = new CandidateHelper();
        reporter.setCandidate(this.reporter);
        reporter.setCandidateId(this.reporter.getUserId());
        reporter.setStatus(CandidateHelper.Status.MENOLONG);
        return reporter;
    }

    private void resolveTheCase(Bundle savedInstanceState) {
        theCase = getArguments().getParcelable(KEY_CASE);

        if (theCase == null && savedInstanceState != null) {
            theCase = savedInstanceState.getParcelable(KEY_CASE);
        }

        if (theCase == null) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }
    }

    private void resolveReporter(Bundle savedInstanceState) {
        reporter = getArguments().getParcelable(KEY_REPORTER);

        if (reporter == null && savedInstanceState != null) {
            reporter = savedInstanceState.getParcelable(KEY_REPORTER);
        }

        if (reporter == null) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }
    }

    @OnTextChanged(R.id.field_message)
    public void fieldMessageChanged(CharSequence message) {
        if (message == null || "".equals(message.toString())) {
            buttonSend.setImageResource(R.drawable.ic_grey_send);
        } else {
            buttonSend.setImageResource(R.drawable.ic_send);
        }
    }

    private void onItemHelperClicked(CandidateHelper candidateHelper) {
        Timber.d(candidateHelper.toString());
    }

    @OnClick(R.id.button_send)
    public void sendMessage() {
        if (theCase.getStatus() != Case.Status.DITUTUP) {
            String content = messageField.getText().toString();
            if (!content.isEmpty()) {<commit_msg>Handle helper item click in chat fragmnet<commit_after>/*
 * Copyright (c) 2015 SatuSatuDua.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package id.satusatudua.sigap.ui.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import id.satusatudua.sigap.R;
import id.satusatudua.sigap.data.model.CandidateHelper;
import id.satusatudua.sigap.data.model.Case;
import id.satusatudua.sigap.data.model.Message;
import id.satusatudua.sigap.data.model.User;
import id.satusatudua.sigap.presenter.ChatPresenter;
import id.satusatudua.sigap.ui.HelpingActivity;
import id.satusatudua.sigap.ui.MainActivity;
import id.satusatudua.sigap.ui.ProfileActivity;
import id.satusatudua.sigap.ui.adapter.ChatAdapter;
import id.satusatudua.sigap.ui.adapter.HelperAdapter;
import id.zelory.benih.ui.fragment.BenihFragment;
import id.zelory.benih.ui.view.BenihRecyclerView;

/**
 * Created on : January 13, 2016
 * Author     : zetbaitsu
 * Name       : Zetra
 * Email      : <EMAIL>
 * GitHub     : https://github.com/zetbaitsu
 * LinkedIn   : https://id.linkedin.com/in/zetbaitsu
 */
public class ChatFragment extends BenihFragment implements ChatPresenter.View {
    private static final String KEY_CASE = "extra_case";
    private static final String KEY_REPORTER = "extra_reporter";
    private static final String KEY_DISABLE_CHAT = "extra_disable_chat";


    @Bind(R.id.list_message) BenihRecyclerView listMessage;
    @Bind(R.id.list_helper) BenihRecyclerView listHelper;
    @Bind(R.id.button_helpers) ImageView buttonHelpers;
    @Bind(R.id.divider) View divider;
    @Bind(R.id.field_message) EditText messageField;
    @Bind(R.id.button_send) ImageView buttonSend;
    @Bind(R.id.root_input) LinearLayout rootInput;

    private ChatAdapter chatAdapter;
    private HelperAdapter helperAdapter;
    private Case theCase;
    private User reporter;
    private ChatPresenter chatPresenter;
    private ProgressDialog progressDialog;

    public static ChatFragment newInstance(Case theCase, User reporter) {
        ChatFragment chatFragment = new ChatFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_CASE, theCase);
        bundle.putParcelable(KEY_REPORTER, reporter);
        chatFragment.setArguments(bundle);
        return chatFragment;
    }

    public static ChatFragment newInstance(Case theCase, User reporter, boolean disableChat) {
        ChatFragment chatFragment = new ChatFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_CASE, theCase);
        bundle.putParcelable(KEY_REPORTER, reporter);
        bundle.putBoolean(KEY_DISABLE_CHAT, disableChat);
        chatFragment.setArguments(bundle);
        return chatFragment;
    }

    @Override
    protected int getResourceLayout() {
        return R.layout.fragment_chat;
    }

    @Override
    protected void onViewReady(@Nullable Bundle savedInstanceState) {
        resolveTheCase(savedInstanceState);
        resolveReporter(savedInstanceState);

        boolean disableChat = getArguments().getBoolean(KEY_DISABLE_CHAT, false);
        rootInput.setVisibility(disableChat ? View.GONE : View.VISIBLE);
        listHelper.setVisibility(disableChat ? View.GONE : View.VISIBLE);

        chatAdapter = new ChatAdapter(getActivity());
        listMessage.setUpAsBottomList();
        listMessage.setAdapter(chatAdapter);

        helperAdapter = new HelperAdapter(getActivity());
        listHelper.setUpAsHorizontalList();
        helperAdapter.add(transformReporter());
        listHelper.setAdapter(helperAdapter);
        helperAdapter.setOnItemClickListener((view, position) -> onItemHelperClicked(helperAdapter.getData().get(position)));

        chatPresenter = new ChatPresenter(this, theCase);
        if (savedInstanceState == null) {
            chatPresenter.loadHelper();
        } else {
            chatPresenter.loadState(savedInstanceState);
        }
    }

    private CandidateHelper transformReporter() {
        CandidateHelper reporter = new CandidateHelper();
        reporter.setCandidate(this.reporter);
        reporter.setCandidateId(this.reporter.getUserId());
        reporter.setStatus(CandidateHelper.Status.MENOLONG);
        return reporter;
    }

    private void resolveTheCase(Bundle savedInstanceState) {
        theCase = getArguments().getParcelable(KEY_CASE);

        if (theCase == null && savedInstanceState != null) {
            theCase = savedInstanceState.getParcelable(KEY_CASE);
        }

        if (theCase == null) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }
    }

    private void resolveReporter(Bundle savedInstanceState) {
        reporter = getArguments().getParcelable(KEY_REPORTER);

        if (reporter == null && savedInstanceState != null) {
            reporter = savedInstanceState.getParcelable(KEY_REPORTER);
        }

        if (reporter == null) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }
    }

    @OnTextChanged(R.id.field_message)
    public void fieldMessageChanged(CharSequence message) {
        if (message == null || "".equals(message.toString())) {
            buttonSend.setImageResource(R.drawable.ic_grey_send);
        } else {
            buttonSend.setImageResource(R.drawable.ic_send);
        }
    }

    private void onItemHelperClicked(CandidateHelper candidateHelper) {
        startActivity(ProfileActivity.generateIntent(getActivity(), candidateHelper.getCandidate()));
    }

    @OnClick(R.id.button_send)
    public void sendMessage() {
        if (theCase.getStatus() != Case.Status.DITUTUP) {
            String content = messageField.getText().toString();
            if (!content.isEmpty()) {
{'fraction_non_alphanumeric': 0.09565151743922694, 'fraction_numerical': 0.0021138456892646836, 'mean_word_length': 3.4935549525101766, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1375794', 'n_tokens_mistral': 3788, 'n_tokens_neox': 3685, 'n_words': 801}