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

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

import java.util.List;

import kz.arta.synergy.workflow.mobile.android.R;
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.documents.DocumentConst;
import kz.arta.synergy.workflow.mobile.android.documents.common.models.DocumentItem;

/**
 * Created by neox on 05.03.14.
 * Адаптер документов для планшета (фильтр "Все")
 */
public class AdapterDocumentsAllTablet extends ArrayAdapter<DocumentItem> {

    private String filterType;
    private Context context;
    private List<DocumentItem> documents;
    private boolean childList;

    public AdapterDocumentsAllTablet(Context context, List<DocumentItem> documents, boolean childList, int id, String filterType) {
        super(context, id, documents);
        this.context = context;
        this.documents = documents;
        this.childList = childList;
        this.filterType = filterType;
    }

    @Override
    public View getView(int position, View rowView, ViewGroup parent) {
        ViewHolder holder;
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.item_documents_list_all_tablet, parent, false);
            holder = new ViewHolder();
            holder.name = (TextView) rowView.findViewById(R.id.document_name);
            holder.documentIcon = (ImageView) rowView.findViewById(R.id.document_icon_document_item);
            holder.inProgress = (ImageView) rowView.findViewById(R.id.in_progress_icon_document_item);
            holder.fromToLabel = (TextView) rowView.findViewById(R.id.from_label_document_item);
            holder.fromToUser = (TextView) rowView.findViewById(R.id.from_user_document_item);
            holder.fromToLabel2 = (TextView) rowView.findViewById(R.id.from_label2_document_item);
            holder.fromToUser2 = (TextView) rowView.findViewById(R.id.from_user2_document_item);
            holder.subworksIcon = (ImageView) rowView.findViewById(R.id.document_subworks_icon);
            holder.titleDate = (TextView) rowView.findViewById(R.id.create_date_label_document_item_tablet);
            holder.date = (TextView) rowView.findViewById(R.id.create_date_document_item_tablet);
            holder.gradient1 = (ImageView) rowView.findViewById(R.id.gradient1_documents_item_tablet_all);
            holder.gradient2 = (ImageView) rowView.findViewById(R.id.gradient2_documents_item_tablet_all);
            holder.front = (RelativeLayout) rowView.findViewById(R.id.front);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        // жирность имени документа
        if (documents.get(position).getIsNew().equals("true")) {
            holder.name.setTypeface(Fonts.getMedium(context));
        } else {
            holder.name.setTypeface(Fonts.getRoman(context));
        }

        // цвет имени документа
        holder.name.setTextColor(context.getResources().getColor(R.color.default_color_work));

        // имя документа
        if (!documents.get(position).getContent().isEmpty()) {
            Spannable ss1 = new SpannableString(documents.get(position).getName() + " - " + documents.get(position).getContent());
            ss1.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.default_color_work)), 0,
                    documents.get(position).getName().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.name.setText(ss1);
            holder.name.setTextColor(context.getResources().getColor(R.color.document_item_content_color));
        } else {
            holder.name.setText(documents.get(position).getName());
            holder.name.setTextColor(context.getResources().getColor(R.color.default_color_work));
        }

        // признак происхождения
        if (documents.get(position).getHasResolution().equals("true")) {
            holder.documentIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.resolution));
        } else {
            if (documents.get(position).getType().equals(DocumentConst.DOCUMENT)) {
                holder.documentIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.document_icon));
            } else if (documents.get(position).getType().equals(DocumentConst.EMAIL)) {
                holder.documentIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.email_icon));
            }
        }

        // признак нахождения документа в процессе
        if (documents.get(position).getInProgress().equals("true")) {
            holder.inProgress.setVisibility(View.VISIBLE);
        } else {
            holder.inProgress.setVisibility(View.GONE);
        }

        // есть ли дочерние работы у документа
        if (documents.get(position).getHasChildren().equals("true")) {
            holder.subworksIcon.setVisibility(View.VISIBLE);
        } else {
            holder.subworksIcon.setVisibility(View.GONE);
        }

        switch (filterType) {
            case DocumentConst.USER_RECEIVED_DOCUMENTS:
                if (!TextUtils.isEmpty(documents.get(position).getFrom())) {
                    holder.fromToLabel.setTypeface(Fonts.getRoman(context));
                    holder.fromToLabel.setText("От: ");
                    holder.fromToLabel.setVisibility(View.VISIBLE);
                    holder.fromToUser.setTypeface(Fonts.getRoman(context));
                    holder.fromToUser.setText(documents.get(position).getFrom());
                    holder.fromToUser.setVisibility(View.VISIBLE);
                    holder.fromToLabel2.setVisibility(View.GONE);
                    holder.fromToUser2.setVisibility(View.GONE);
                    holder.titleDate.setTypeface(Fonts.getRoman(context));
                    holder.titleDate.setText("Получено: ");
                } else {
                    holder.fromToLabel.setVisibility(View.GONE);
                    holder.fromToUser.setVisibility(View.GONE);
                    holder.fromToLabel2.setVisibility(View.GONE);
                    holder.fromToUser2.setVisibility(View.GONE);
                    holder.titleDate.setTypeface(Fonts.getRoman(context));
                    holder.titleDate.setText("Создано: ");
                }
                break;
            case DocumentConst.USER_SENT_DOCUMENTS:
                if (!TextUtils.isEmpty(documents.get(position).getTo())) {
                    holder.fromToLabel.setTypeface(Fonts.getRoman(context));
                    holder.fromToLabel.setText("Кому: ");
                    holder.fromToLabel.setVisibility(View.VISIBLE);
                    holder.fromToUser.setTypeface(Fonts.getRoman(context));
                    holder.fromToUser.setText(documents.get(position).getTo());
                    holder.fromToUser.setVisibility(View.VISIBLE);
                    holder.fromToLabel2.setVisibility(View.GONE);
                    holder.fromToUser2.setVisibility(View.GONE);
                    holder.titleDate.setTypeface(Fonts.getRoman(context));
                    holder.titleDate.setText("Отправлено: ");
                } else {
                    holder.fromToLabel.setVisibility(View.GONE);
                    holder.fromToUser.setVisibility(View.GONE);
                    holder.fromToLabel2.setVisibility(View.GONE);
                    holder.fromToUser2.setVisibility(View.GONE);
                    holder.titleDate.setTypeface(Fonts.getRoman(context));
                    holder.titleDate.setText("Создано: ");
                }
                break;
            default:
                if (!TextUtils.isEmpty(documents.get(position).getFrom()) && !TextUtils.isEmpty(documents.get(position).getTo())) {
                    holder.fromToLabel.setTypeface(Fonts.getRoman(context));
                    holder.fromToLabel.setText("От: ");
                    holder.fromToLabel.setVisibility(View.VISIBLE);
                    holder.fromToUser.setTypeface(Fonts.getRoman(context));
                    holder.fromToUser.setText(documents.get(position).getFrom());
                    holder.fromToUser.setVisibility(View.VISIBLE);
                    holder.fromToLabel2.setTypeface(Fonts.getRoman(context));
                    holder.fromToLabel2.setText("Кому: ");
                    holder.fromToLabel2.setVisibility(View.VISIBLE);
                    holder.fromToUser2.setTypeface(Fonts.getRoman(context));
                    holder.fromToUser2.setText(documents.get(position).getTo());
                    holder.fromToUser2.setVisibility(View.VISIBLE);
                    holder.titleDate.setTypeface(Fonts.getRoman(context));
                    holder.titleDate.setText("Отправлено: ");
                } else if (!TextUtils.isEmpty(documents.get(position).getTo())) {
                    holder.fromToLabel.setTypeface(Fonts.getRoman(context));
                    holder.fromToLabel.setText("Кому: ");
                    holder.fromToLabel.setVisibility(View.VISIBLE);
                    holder.fromToUser.setTypeface(Fonts.getRoman(context));
                    holder.fromToUser.setText(documents.get(position).getTo());
                    holder.fromToUser.setVisibility(View.VISIBLE);
                    holder.fromToLabel2.setVisibility(View.GONE);
                    holder.fromToUser2.setVisibility(View.GONE);
                    holder.titleDate.setTypeface(Fonts.getRoman(context));
                    holder.titleDate.setText("Отправлено: ");
                } else if (!TextUtils.isEmpty(documents.get(position).getFrom())) {
                    holder.fromToLabel.setTypeface(Fonts.getRoman(context));
                    holder.fromToLabel.setText("От: ");
                    holder.fromToLabel.setVisibility(View.VISIBLE);
                    holder.fromToUser.setTypeface(Fonts.getRoman(context));
                    holder.fromToUser.setText(documents.get(position).getFrom());
                    holder.fromToUser.setVisibility(View.VISIBLE);
                    holder.fromToLabel2.setVisibility(View.GONE);
                    holder.fromToUser2.setVisibility(View.GONE);
                    holder.titleDate.setTypeface(Fonts.getRoman(context));
                    holder.titleDate.setText("Получено: ");
                } else {
                    holder.fromToLabel.setVisibility(View.GONE);
                    holder.fromToUser.setVisibility(View.GONE);
                    holder.fromToLabel2.setVisibility(View.GONE);
                    holder.fromToUser2.setVisibility(View.GONE);
                    holder.titleDate.setTypeface(Fonts.getRoman(context));
                    holder.titleDate.setText("Создано: ");
                }
                break;
        }

        // дата
        holder.date.setText(DateUtils.getFullDateDocumentItem(documents.get(position).getCreateDate()));
        holder.date.setTypeface(Fonts.getRoman(context));

        // если открыты дочерние работы - делаем перую работу серым цветом
        if (childList && position == 0) {
            holder.subworksIcon.setVisibility(View.GONE);
            holder.gradient1.setBackgroundResource(R.drawable.child_list_gradient);
            holder.gradient2.setBackgroundResource(R.drawable.child_list_gradient);
            holder.name.setTypeface(Fonts.getMedium(context));
            holder.front.setBackgroundColor(context.getResources().getColor(R.color.first_item_child_documents_color));
        } else {
            holder.gradient1.setBackgroundResource(R.drawable.main_list_gradient);
            holder.gradient2.setBackgroundResource(R.drawable.main_list_gradient);
            holder.front.setBackgroundColor(context.getResources().getColor(R.color.background_documents_list_color));
        }

        return rowView;
    }

    static class ViewHolder {
        // компоненты для неполной плашки
        ImageView documentIcon;
        ImageView inProgress;
        TextView name;
        TextView fromToLabel;
        TextView fromToUser;
        TextView fromToLabel2;
        TextView fromToUser2;
        ImageView subworksIcon;
        ImageView gradient1;
        ImageView gradient2;
        TextView titleDate;
        TextView date;
        RelativeLayout front;
    }
}
