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 AdapterDocumentsJournal extends ArrayAdapter<DocumentItem> {

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

    public AdapterDocumentsJournal(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, parent, false);
            holder = new ViewHolder();

            // полная и неполная плашка
            holder.partItem = (RelativeLayout) rowView.findViewById(R.id.part_item);
            holder.fullItem = (RelativeLayout) rowView.findViewById(R.id.full_document_item);

            // компоненты неполной плашки
            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.iconRegistration = (ImageView) rowView.findViewById(R.id.registration_icon_document_item);
            holder.subworksIcon = (ImageView) rowView.findViewById(R.id.document_subworks_icon);
            holder.gradient1 = (ImageView) rowView.findViewById(R.id.gradient1_documents_item_journal);
            holder.gradient2 = (ImageView) rowView.findViewById(R.id.gradient2_documents_item_journal);

            // компоненты полной плашки
            holder.iconTypeFull = (ImageView) rowView.findViewById(R.id.document_icon_document_type_full);
            holder.iconResolutionFull = (ImageView) rowView.findViewById(R.id.document_icon_document_resolution_full);
            holder.iconControlledFull = (ImageView) rowView.findViewById(R.id.document_icon_document_controlled_full);
            holder.iconDefectiveFull = (ImageView) rowView.findViewById(R.id.document_icon_document_defective_full);
            holder.iconRejectedFull = (ImageView) rowView.findViewById(R.id.document_icon_document_rejected_full);
            holder.documentNumberFull = (TextView) rowView.findViewById(R.id.document_number_document_item_full);
            holder.documentNameFull = (TextView) rowView.findViewById(R.id.document_name_full);
            holder.registrationPanel = (RelativeLayout) rowView.findViewById(R.id.document_full_item_registration_panel);
            holder.registrationTitle = (TextView) rowView.findViewById(R.id.full_document_item_registration_title);
            holder.iconStatusFull = (ImageView) rowView.findViewById(R.id.status_icon_document_item_full);
            holder.iconRegistrationFull = (ImageView) rowView.findViewById(R.id.registration_icon_document_item_full);
            holder.registeredUserTitle = (TextView) rowView.findViewById(R.id.full_document_item_registered_user_title);
            holder.registeredDateTitle = (TextView) rowView.findViewById(R.id.full_document_item_registered_date_title);
            holder.registeredUnsubscribeTitle = (TextView) rowView.findViewById(R.id.full_document_item_registered_unsubscribe_title);
            holder.registeredResUsersTitle = (TextView) rowView.findViewById(R.id.full_document_item_registered_res_users_title);
            holder.registeredUser = (TextView) rowView.findViewById(R.id.full_document_item_registered_user);
            holder.registeredDate = (TextView) rowView.findViewById(R.id.full_document_item_registered_date);
            holder.registeredUnsubscribe = (TextView) rowView.findViewById(R.id.full_document_item_registered_unsubscribe);
            holder.registeredResUsers = (TextView) rowView.findViewById(R.id.full_document_item_registered_res_users);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        // если открыта полная плашка
        if (documents.get(position).isOpened()) {

            // тип документа
            if (documents.get(position).getType().equals(DocumentConst.DOCUMENT)) {
                holder.iconTypeFull.setImageDrawable(context.getResources().getDrawable(R.drawable.document_icon));
            } else if (documents.get(position).getType().equals(DocumentConst.EMAIL)) {
                holder.iconTypeFull.setImageDrawable(context.getResources().getDrawable(R.drawable.email_icon));
            }

            // резолюция ли
            if (documents.get(position).getHasResolution().equals("true")) {
                holder.iconResolutionFull.setVisibility(View.VISIBLE);
            } else {
                holder.iconResolutionFull.setVisibility(View.GONE);
            }

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

            // бракован ли
            if (documents.get(position).getDefective().equals("true")) {
                holder.iconDefectiveFull.setVisibility(View.VISIBLE);
            } else {
                holder.iconDefectiveFull.setVisibility(View.GONE);
            }

            // отклонен ли
            if (documents.get(position).getStatusID().equals(DocumentConst.REJECTED)) {
                holder.iconRejectedFull.setVisibility(View.VISIBLE);
            } else {
                holder.iconRejectedFull.setVisibility(View.GONE);
            }

            // если документ зарегистрирован - отображаем его номер
            if (documents.get(position).getRegistered().equals("true")) {
                if (documents.get(position).getDocTypeID().equals(DocumentConst.INTERNAL_DOCS)) {
                    holder.documentNumberFull.setText(documents.get(position).getNumber() + " - " + documents.get(position).getAuthor());
                } else {
                    if (!TextUtils.isEmpty(documents.get(position).getCorrespondentOrg())) {
                        holder.documentNumberFull.setText(documents.get(position).getNumber() + " - " + documents.get(position).getCorrespondentOrg());
                    } else if (!TextUtils.isEmpty(documents.get(position).getCorrespondent())) {
                        holder.documentNumberFull.setText(documents.get(position).getNumber() + " - " + documents.get(position).getCorrespondent());
                    } else {
                        holder.documentNumberFull.setText(documents.get(position).getNumber());
                    }
                }
                holder.documentNumberFull.setTypeface(Fonts.getMedium(context));
                holder.documentNumberFull.setVisibility(View.VISIBLE);
            } else {
                holder.documentNumberFull.setVisibility(View.GONE);
            }

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

            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.documentNameFull.setText(ss1);
                holder.documentNameFull.setTextColor(context.getResources().getColor(R.color.document_item_content_color));
            } else {
                holder.documentNameFull.setText(documents.get(position).getName());
                holder.documentNameFull.setTextColor(documentColor);
            }

            // если документ зарегистрирован
            if (documents.get(position).getRegistered().equals("true")) {
                holder.registrationPanel.setVisibility(View.VISIBLE);
                holder.registrationTitle.setTypeface(Fonts.getRoman(context));

                if (documents.get(position).getStatusID().equals(DocumentConst.NOT_STARTED)) {
                    holder.iconRegistrationFull.setImageDrawable(context.getResources().getDrawable(R.drawable.in_registration_icon));
                } else if (documents.get(position).getRegistered().equals("true")) {
                    holder.iconRegistrationFull.setImageDrawable(context.getResources().getDrawable(R.drawable.registered_icon));
                }

                if (documents.get(position).getFinished().equals("true")) {
                    holder.iconStatusFull.setImageDrawable(context.getResources().getDrawable(R.drawable.progress_done_icon));
                } else {
                    holder.iconStatusFull.setImageDrawable(context.getResources().getDrawable(R.drawable.calendar_status_icon));
                }

                if (!TextUtils.isEmpty(documents.get(position).getUser())) {
                    holder.registeredUnsubscribeTitle.setText(person.getSettings().getSignedOff() + ": ");
                    holder.registeredUnsubscribeTitle.setTypeface(Fonts.getRoman(context));
                    holder.registeredUnsubscribe.setTypeface(Fonts.getRoman(context));
                    holder.registeredUnsubscribe.setText(documents.get(position).getUser());
                    holder.registeredUnsubscribeTitle.setVisibility(View.VISIBLE);
                    holder.registeredUnsubscribe.setVisibility(View.VISIBLE);
                } else {
                    holder.registeredUnsubscribeTitle.setVisibility(View.GONE);
                    holder.registeredUnsubscribe.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(documents.get(position).getRegDate())) {
                    holder.registeredDateTitle.setTypeface(Fonts.getRoman(context));
                    holder.registeredDate.setTypeface(Fonts.getRoman(context));
                    holder.registeredDate.setText(DateUtils.getDateDocumentItem(documents.get(position).getRegDate()));
                    holder.registeredDateTitle.setVisibility(View.VISIBLE);
                    holder.registeredDate.setVisibility(View.VISIBLE);
                } else {
                    holder.registeredDateTitle.setVisibility(View.GONE);
                    holder.registeredDate.setVisibility(View.GONE);
                }

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

                if (!TextUtils.isEmpty(documents.get(position).getRegUser())) {
                    holder.registeredUserTitle.setTypeface(Fonts.getRoman(context));
                    holder.registeredUser.setTypeface(Fonts.getRoman(context));
                    holder.registeredUser.setText(documents.get(position).getRegUser());
                    holder.registeredUserTitle.setVisibility(View.VISIBLE);
                    holder.registeredUser.setVisibility(View.VISIBLE);
                } else {
                    holder.registeredUserTitle.setVisibility(View.GONE);
                    holder.registeredUser.setVisibility(View.GONE);
                }

            } else {
                holder.registrationPanel.setVisibility(View.GONE);
            }

            holder.fullItem.setVisibility(View.VISIBLE);
            holder.partItem.setVisibility(View.GONE);

        } else {

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

            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);
            } else {
                holder.documentNumber.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).getDefective().equals("true")) {
                holder.iconStatus.setImageDrawable(context.getResources().getDrawable(R.drawable.deffective_icon));
                holder.iconStatus.setVisibility(View.VISIBLE);
            } else {
                if (documents.get(position).getRegistered().equals("true")) {
                    if (documents.get(position).getFinished().equals("true")) {
                        holder.iconStatus.setImageDrawable(context.getResources().getDrawable(R.drawable.progress_done_icon));
                        holder.iconStatus.setVisibility(View.VISIBLE);
                    } else {
                        holder.iconStatus.setImageDrawable(context.getResources().getDrawable(R.drawable.calendar_status_icon));
                        holder.iconStatus.setVisibility(View.VISIBLE);
                    }
                } else {
                    holder.iconStatus.setVisibility(View.GONE);
                }
            }

            if (documents.get(position).getStatusID().equals(DocumentConst.REJECTED)) {
                holder.iconRegistration.setImageDrawable(context.getResources().getDrawable(R.drawable.draft_icon));
            } else {
                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 (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.partItem.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.partItem.setBackgroundColor(context.getResources().getColor(R.color.background_documents_list_color));
            }

            holder.fullItem.setVisibility(View.GONE);
            holder.partItem.setVisibility(View.VISIBLE);
        }

        return rowView;
    }

    static class ViewHolder {

        // полная и неполная плашка
        RelativeLayout partItem;
        RelativeLayout fullItem;

        // компоненты для неполной плашки
        ImageView iconType;
        ImageView iconStatus;
        ImageView iconRegistration;
        TextView documentNumber;
        TextView documentName;
        ImageView subworksIcon;
        ImageView gradient1;
        ImageView gradient2;

        // компоненты для полной плашки
        ImageView iconTypeFull;
        ImageView iconResolutionFull;
        ImageView iconControlledFull;
        ImageView iconDefectiveFull;
        ImageView iconRejectedFull;
        TextView documentNumberFull;
        TextView documentNameFull;

        RelativeLayout registrationPanel;
        TextView registrationTitle;
        ImageView iconStatusFull;
        ImageView iconRegistrationFull;

        TextView registeredUserTitle;
        TextView registeredDateTitle;
        TextView registeredUnsubscribeTitle;
        TextView registeredResUsersTitle;

        TextView registeredUser;
        TextView registeredDate;
        TextView registeredUnsubscribe;
        TextView registeredResUsers;
    }
}
