package kz.arta.synergy.workflow.mobile.android.comments.activities;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.devspark.appmsg.AppMsg;
import com.pnikosis.materialishprogress.ProgressWheel;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.comments.manager.AddCommentRequest;
import kz.arta.synergy.workflow.mobile.android.common.ActivityConst;
import kz.arta.synergy.workflow.mobile.android.common.SettingsConst;
import kz.arta.synergy.workflow.mobile.android.common.models.Person;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;

/**
 * Created by neox on 10.04.14.
 * Добавление комментария
 */
public class AddCommentActivity extends FragmentActivity {

    /**
     * Прогрессбар
     */
    private ProgressWheel progressBar;

    /**
     * Данные для авторизированного пользователя
     */
    private Person person;

    /**
     * Настройки
     */
    private SharedPreferences settings;

    /**
     * Активити
     */
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_comment_activity);

        activity = this;

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
            ((TextView) findViewById(R.id.title_action_bar_add_comment)).setTypeface(Fonts.getMedium(activity));
        }

        Intent intent = getIntent();
        person = (Person) intent.getSerializableExtra(ActivityConst.PERSON);
        final String commentID = intent.getStringExtra(ActivityConst.COMMENT_ID);
        final String workID = intent.getStringExtra(ActivityConst.WORK_ID);
        final String workName = intent.getStringExtra(ActivityConst.WORK_NAME);
        final String documentID = intent.getStringExtra(ActivityConst.DOCUMENT_ID);
        final String commentText = intent.getStringExtra(ActivityConst.COMMENT_TEXT);
        final String commentTime = intent.getStringExtra(ActivityConst.COMMENT_TIME);
        final String commentType = intent.getStringExtra(ActivityConst.COMMENT_TYPE);
        final boolean edit = intent.getBooleanExtra(ActivityConst.EDIT, false);

        settings = activity.getSharedPreferences(SettingsConst.DEFAULT_NAME_SETTINGS, Context.MODE_PRIVATE);

        progressBar = (ProgressWheel) findViewById(R.id.add_comment_progress_bar);

        final EditText comment = (EditText) findViewById(R.id.add_comment_edit_text);
        if (edit) {
            comment.setText(commentText);
        }

        final Button saveComment = (Button) findViewById(R.id.add_comment_button);
        saveComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = comment.getText().toString();
                if (text.trim().length() != 0 && text.length() != 0) {
                    // создаем объект, заполянем его новостями, и отправляем запрос на отправку комментария
                    AddCommentRequest addComment = new AddCommentRequest();
                    addComment.setActivity(activity);
                    addComment.setPerson(person);
                    addComment.setProgressBar(progressBar);
                    addComment.setmSettings(settings);
                    addComment.setTime(commentTime);
                    addComment.setCommentType(commentType);
                    addComment.setCommentText(text);
                    if (!edit) {
                        addComment.setEdit(false);
                    } else {
                        addComment.setEdit(true);
                        addComment.setCommentID(commentID);
                    }
                    if (workID != null) {
                        addComment.setWorkID(workID);
                        addComment.setName(workName);
                    } else if (documentID != null) {
                        addComment.setDocumentID(documentID);
                        addComment.setName(workName);
                    }
                    addComment.execute();
                } else {
                    AppMsg.makeText(activity, getString(R.string.enter_comment), AppMsg.STYLE_ALERT).show();
                }
            }
        });
    }
}
