package kz.arta.synergy.workflow.mobile.android.documents.resolution.adapters;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.common.models.Person;
import kz.arta.synergy.workflow.mobile.android.common.utils.DateUtils;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;
import kz.arta.synergy.workflow.mobile.android.common.utils.Utils;
import kz.arta.synergy.workflow.mobile.android.documents.resolution.models.ItemResolution;

/**
 * Created by neox on 14.09.14.
 * Адаптер списка пунктов резолюций
 */
public class AdapterResolution extends ArrayAdapter<ItemResolution> {

    /**
     * Список элементов
     */
    private List<ItemResolution> items;

    /**
     * Контекст
     */
    private Context context;

    /**
     * Пользователь
     */
    private Person person;

    public AdapterResolution(Context context, List<ItemResolution> items, Person person) {
        super(context, R.layout.resolution_activity, items);
        this.context = context;
        this.items = items;
        this.person = person;
    }

    @Override
    public View getView(int i, View someView, ViewGroup arg2) {

        LayoutInflater inflater = LayoutInflater.from(context);
        if (someView == null) {
            someView = inflater.inflate(R.layout.item_resolution, arg2, false);
        }

        // текст резолюции
        TextView textResolution = (TextView) someView.findViewById(R.id.text_resolution);
        textResolution.setText(items.get(i).getName());
        textResolution.setTypeface(Fonts.getRoman(context));

        // заголовок "Ответственный"
        TextView responsibleTitle = (TextView) someView.findViewById(R.id.responsible_title_resolution);
        responsibleTitle.setTypeface(Fonts.getThin(context));

        // fucking hardcode...
        Spannable ss1 = new SpannableString(context.getResources().getString(R.string.responsible) + Utils.parseUser(items.get(i).getUser()));
        ss1.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.default_color_activities)), 0,
                context.getResources().getString(R.string.responsible).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        // "Ответственный"
        final TextView responsible = (TextView) someView.findViewById(R.id.responsible_resolution);
        responsible.setTypeface(Fonts.getMedium(context));
        responsible.setText(ss1);

        // заголовок "Исполнители"
        TextView executorsTitle = (TextView) someView.findViewById(R.id.executors_title_resolution);
        executorsTitle.setTypeface(Fonts.getThin(context));

        // fucking hardcode...
        Spannable ss2 = new SpannableString(context.getResources().getString(R.string.choose_executor) + Utils.parseExecutors(items.get(i).getUsers()));
        ss2.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.default_color_activities)), 0,
                context.getResources().getString(R.string.choose_executor).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        // "Исполнители"
        TextView executors = (TextView) someView.findViewById(R.id.executors_resolution);
        executors.setTypeface(Fonts.getMedium(context));
        executors.setText(ss2);

        //  заголовок "Завершение"
        TextView completedTitle = (TextView) someView.findViewById(R.id.completed_title_resolution);
        completedTitle.setTypeface(Fonts.getThin(context));

        // "Завершение"
        TextView completed = (TextView) someView.findViewById(R.id.completed_resolution);
        completed.setTypeface(Fonts.getMedium(context));

        if (items.get(i).getFinishdate() != null && items.get(i).getFinishdate().getDate() != null) {
            completed.setText(DateUtils.getResolutionDate(items.get(i).getFinishdate().getDate(), context));
        }

        // заголовок "Тип работы"
        TextView typeWorkTitle = (TextView) someView.findViewById(R.id.type_work_title_resolution);
        typeWorkTitle.setTypeface(Fonts.getThin(context));

        // "Тип работы"
        TextView typeWork = (TextView) someView.findViewById(R.id.type_work_resolution);
        typeWork.setTypeface(Fonts.getMedium(context));
        typeWork.setText(items.get(i).getItemName());

        // если форма завершения результат работы - не отображаем данную плашку
        if (Utils.getFormCompletion(items.get(i).getCompletionFormID(), person, context, true).equals(context.getString(R.string.work_result))) {
            someView.findViewById(R.id.form_completion_title_resolution).setVisibility(View.GONE);
            someView.findViewById(R.id.form_completion_resolution).setVisibility(View.GONE);
        } else {
            // заголовок "Форма завершения"
            TextView formCompletionTitle = (TextView) someView.findViewById(R.id.form_completion_title_resolution);
            formCompletionTitle.setTypeface(Fonts.getThin(context));
            formCompletionTitle.setVisibility(View.VISIBLE);
            formCompletionTitle.setText(formCompletionTitle.getText() + ":");
            // "Форма завершения"
            TextView formCompletion = (TextView) someView.findViewById(R.id.form_completion_resolution);
            formCompletion.setTypeface(Fonts.getMedium(context));
            formCompletion.setText(Utils.getFormCompletion(items.get(i).getCompletionFormID(), person, context, true));
            formCompletion.setVisibility(View.VISIBLE);
        }
        return someView;
    }
}
