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

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

    private Person person;
    private Context context;
    private List<DocumentItem> documents;
    private boolean childList;

    public AdapterDocumentsJournalTablet(Context context, List<DocumentItem> documents, boolean childList, int id, Person person) {
        super(context, id, documents);
        this.context = context;
        this.documents = documents;
        this.childList = childList;
        this.person = person;
    }

    @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_journal_tablet, parent, false);
            holder = new ViewHolder();
            // компоненты неполной плашки
            holder.documentName = (TextView) rowView.findViewById(R.id.document_name);
            holder.documentNumber = (TextView) rowView.findViewById(R.id.document_number_document_item);
            holder.iconType = (ImageView) rowView.findViewById(R.id.document_icon_document_item);
            holder.iconStatus = (ImageView) rowView.findViewById(R.id.status_icon_document_item);
            holder.iconDefective = (ImageView) rowView.findViewById(R.id.deffective_icon_document_item);
            holder.iconDraft = (ImageView) rowView.findViewById(R.id.draft_icon_document_item);
            holder.iconControlled = (ImageView) rowView.findViewById(R.id.controlled_icon_document_item);
            holder.iconRegistration = (ImageView) rowView.findViewById(R.id.registration_icon_document_item);
            holder.subworksIcon = (ImageView) rowView.findViewById(R.id.document_subworks_icon);
            holder.resUsersLabel = (TextView) rowView.findViewById(R.id.on_executor_document_item_tablet_label);
            holder.resUsers = (TextView) rowView.findViewById(R.id.on_executor_document_item_tablet);
            holder.unsubscribedLabel = (TextView) rowView.findViewById(R.id.unsubscribed_document_item_tablet_label);
            holder.unsubscribed = (TextView) rowView.findViewById(R.id.unsubscribed_document_item_tablet);
            holder.iconRegUser = (ImageView) rowView.findViewById(R.id.registered_user_icon_document_item);
            holder.labelRegUser = (TextView) rowView.findViewById(R.id.registered_user_label_document_item);
            holder.regDate = (TextView) rowView.findViewById(R.id.document_item_tablet_registration_date);
            holder.gradient1 = (ImageView) rowView.findViewById(R.id.gradient1_documents_item_tablet_journal);
            holder.gradient2 = (ImageView) rowView.findViewById(R.id.gradient2_documents_item_tablet_journal);
            holder.separator = rowView.findViewById(R.id.item_documents_list_journal_separator);
            holder.front = (RelativeLayout) rowView.findViewById(R.id.front);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

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

        if (documents.get(position).getRegistered().equals("true") && !TextUtils.isEmpty(documents.get(position).getUser())) {
            holder.separator.setVisibility(View.VISIBLE);
        } else {
            holder.separator.setVisibility(View.GONE);
        }


        int documentColor;
        if (documents.get(position).getStatusID().equals(DocumentConst.REJECTED) && documents.get(position).getDefective().equals("true")) {
            documentColor = context.getResources().getColor(R.color.defective_color_document_name);
        } else if (documents.get(position).getStatusID().equals(DocumentConst.REJECTED)) {
            documentColor = context.getResources().getColor(R.color.default_color_work);
        } else if (documents.get(position).getDefective().equals("true")) {
            documentColor = context.getResources().getColor(R.color.defective_color_document_name);
        } else {
            if (documents.get(position).getFinished().equals("true") || documents.get(position).getStatusID().equals(DocumentConst.NOT_STARTED)) {
                documentColor = context.getResources().getColor(R.color.default_color_work);
            } else {
                if (documents.get(position).getIsExpired().equals("true")) {
                    documentColor = context.getResources().getColor(R.color.expired_color_work);
                } else {
                    if (documents.get(position).getIsSoonExpired().equals("true")) {
                        documentColor = context.getResources().getColor(R.color.soon_expired_color_work);
                    } else {
                        documentColor = 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(documentColor), 0, documents.get(position).getName().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.documentName.setText(ss1);
            holder.documentName.setTextColor(context.getResources().getColor(R.color.document_item_content_color));
        } else {
            holder.documentName.setText(documents.get(position).getName());
            holder.documentName.setTextColor(documentColor);
        }

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

        // если документ зарегистрирован
        if (documents.get(position).getRegistered().equals("true")) {

            // номер документа
            if (documents.get(position).getDocTypeID().equals(DocumentConst.INTERNAL_DOCS)) {
                holder.documentNumber.setText(documents.get(position).getNumber() + " - " + documents.get(position).getAuthor());
            } else {
                if (!TextUtils.isEmpty(documents.get(position).getCorrespondentOrg())) {
                    holder.documentNumber.setText(documents.get(position).getNumber() + " - " + documents.get(position).getCorrespondentOrg());
                } else if (!TextUtils.isEmpty(documents.get(position).getCorrespondent())) {
                    holder.documentNumber.setText(documents.get(position).getNumber() + " - " + documents.get(position).getCorrespondent());
                } else {
                    holder.documentNumber.setText(documents.get(position).getNumber());
                }
            }
            holder.documentNumber.setTypeface(Fonts.getMedium(context));
            holder.documentNumber.setVisibility(View.VISIBLE);

            // дата регистрации документа
            if (!TextUtils.isEmpty(documents.get(position).getRegDate())) {
                holder.regDate.setText(DateUtils.getDateDocumentItem(documents.get(position).getRegDate()));
                holder.regDate.setTypeface(Fonts.getRoman(context));
                holder.regDate.setVisibility(View.VISIBLE);
            } else {
                holder.regDate.setVisibility(View.GONE);
            }

            // если документ завершенный
            holder.iconStatus.setVisibility(View.VISIBLE);
            if (documents.get(position).getFinished().equals("true")) {
                holder.iconStatus.setImageDrawable(context.getResources().getDrawable(R.drawable.progress_done_icon));
            } else {
                holder.iconStatus.setImageDrawable(context.getResources().getDrawable(R.drawable.calendar_status_icon));
            }

            if (!TextUtils.isEmpty(documents.get(position).getResUsers())) {
                holder.resUsersLabel.setTypeface(Fonts.getMedium(context));
                holder.resUsersLabel.setVisibility(View.VISIBLE);
                holder.resUsers.setText(documents.get(position).getResUsers());
                holder.resUsers.setTypeface(Fonts.getMedium(context));
                holder.resUsers.setVisibility(View.VISIBLE);
            } else {
                holder.resUsersLabel.setVisibility(View.GONE);
                holder.resUsers.setVisibility(View.GONE);
            }

            // пользователь зарегистрировавший документ
            if (!TextUtils.isEmpty(documents.get(position).getRegUser())) {
                holder.labelRegUser.setTypeface(Fonts.getRoman(context));
                holder.labelRegUser.setText(documents.get(position).getRegUser());
                holder.labelRegUser.setVisibility(View.VISIBLE);
                holder.iconRegUser.setVisibility(View.VISIBLE);
            } else {
                holder.labelRegUser.setVisibility(View.GONE);
                holder.iconRegUser.setVisibility(View.GONE);
            }

            // отписавший
            if (!TextUtils.isEmpty(documents.get(position).getUser())) {
                holder.unsubscribedLabel.setText(person.getSettings().getSignedOff() + ": ");
                holder.unsubscribedLabel.setTypeface(Fonts.getRoman(context));
                holder.unsubscribedLabel.setVisibility(View.VISIBLE);
                holder.unsubscribed.setText(documents.get(position).getUser());
                holder.unsubscribed.setTypeface(Fonts.getRoman(context));
                holder.unsubscribed.setVisibility(View.VISIBLE);
            } else {
                holder.unsubscribedLabel.setVisibility(View.GONE);
                holder.unsubscribed.setVisibility(View.GONE);
            }

        } else {
            holder.documentNumber.setVisibility(View.GONE);
            holder.iconStatus.setVisibility(View.GONE);
            holder.resUsersLabel.setVisibility(View.GONE);
            holder.resUsers.setVisibility(View.GONE);
            holder.labelRegUser.setVisibility(View.GONE);
            holder.iconRegUser.setVisibility(View.GONE);
            holder.unsubscribedLabel.setVisibility(View.GONE);
            holder.unsubscribed.setVisibility(View.GONE);
            holder.regDate.setVisibility(View.GONE);
        }

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

        // отклоненный
        if (documents.get(position).getStatusID().equals(DocumentConst.REJECTED)) {
            holder.iconDraft.setVisibility(View.VISIBLE);
            holder.iconRegistration.setVisibility(View.GONE);
        } else {
            holder.iconDraft.setVisibility(View.GONE);
            holder.iconRegistration.setVisibility(View.VISIBLE);
            if (documents.get(position).getStatusID().equals(DocumentConst.NOT_STARTED)) {
                holder.iconRegistration.setImageDrawable(context.getResources().getDrawable(R.drawable.in_registration_icon));
            } else if (documents.get(position).getRegistered().equals("true")) {
                holder.iconRegistration.setImageDrawable(context.getResources().getDrawable(R.drawable.registered_icon));
            }
        }

        // деффектный
        if (documents.get(position).getDefective().equals("true")) {
            holder.iconDefective.setVisibility(View.VISIBLE);
        } else {
            holder.iconDefective.setVisibility(View.GONE);
        }

        // контрольный
        if (documents.get(position).getControlled().equals("true")) {
            holder.iconControlled.setVisibility(View.VISIBLE);
        } else {
            holder.iconControlled.setVisibility(View.GONE);
        }

        // если открыты дочерние работы - делаем перую работу серым цветом
        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.documentName.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 iconType;
        ImageView iconStatus;
        ImageView iconRegistration;
        ImageView iconControlled;
        ImageView iconDefective;
        ImageView iconDraft;
        ImageView iconRegUser;
        TextView labelRegUser;
        TextView documentNumber;
        TextView documentName;
        ImageView subworksIcon;
        TextView resUsersLabel;
        TextView resUsers;
        TextView unsubscribedLabel;
        TextView unsubscribed;
        TextView regDate;
        ImageView gradient1;
        ImageView gradient2;
        RelativeLayout front;
        View separator;
    }
}
