package kz.arta.synergy.workflow.mobile.android.works.adapters;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
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.Fonts;
import kz.arta.synergy.workflow.mobile.android.works.models.WorkItem;

/**
 * Created by neox on 05.03.14.
 * Адаптер списка работ
 */
public class AdapterWorkList extends ArrayAdapter<WorkItem> {

    private Context context;
    private List<WorkItem> works;
    private boolean childList;
    private Person person;

    public AdapterWorkList(Context context, List<WorkItem> works, boolean childList, int id, Person person) {
        super(context, id, works);
        this.context = context;
        this.works = works;
        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_work_list, parent, false);
            holder = new ViewHolder();
            holder.priority = rowView.findViewById(R.id.priority_work_list);
            holder.resolutionImage = (ImageView) rowView.findViewById(R.id.resolution_work_list);
            holder.author = (TextView) rowView.findViewById(R.id.author_work_list);
            holder.calendar = (TextView) rowView.findViewById(R.id.calendar_work_list);
            holder.calendarImage = (ImageView) rowView.findViewById(R.id.calendar_image_work_list);
            holder.progress = (TextView) rowView.findViewById(R.id.progress_work_list);
            holder.progressImage = (ImageView) rowView.findViewById(R.id.progress_image_work_list);
            holder.workText = (TextView) rowView.findViewById(R.id.name_work_list);
            holder.openChildWorks = (ImageView) rowView.findViewById(R.id.subworks_image);
            holder.gradient = (ImageView) rowView.findViewById(R.id.bottom_item_gradient);
            holder.front = (RelativeLayout) rowView.findViewById(R.id.front);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        GradientDrawable shape = new GradientDrawable();
        shape.setCornerRadius(8);

        try {
            shape.setColor(Color.parseColor(person.
                    getPriorityItemHashMap().get(works.get(position).getPriority()).getColor()));
        } catch (Exception ignored) {
        }

        // выставляем приоритет
        holder.priority.setBackgroundDrawable(shape);

        try {
            if (works.get(position).getIs_resolution().equals("true")) {
                holder.resolutionImage.setVisibility(View.VISIBLE);
            } else {
                holder.resolutionImage.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            holder.resolutionImage.setVisibility(View.GONE);
        }

        if (person.getUuid().equals(works.get(position).getUser().getID())) {
            holder.author.setText(works.get(position).getAuthor().getName());
            holder.author.setTypeface(Fonts.getBold(context));
        } else {
            holder.author.setText(works.get(position).getUser().getName());
            holder.author.setTypeface(Fonts.getMedium(context));
        }

        // 2 иконки и 2 текствьюшки
        Object icon1 = null;
        Object icon2 = null;
        String text1 = null;
        String text2 = null;

        // дальше идет проверки для отображения иконок
        String work_state_label = works.get(position).getWork_state_label(); // идентификатор лейбла
        String work_state_icon = works.get(position).getWork_state_icon(); // идентификатор иконки
        switch (work_state_label) {
            case "agreement":
                switch (work_state_icon) {
                    case "progress_mfa_icon":
                        icon2 = R.drawable.progress_mfa_icon;
                        icon1 = R.drawable.calendar_status_icon;
                        text2 = context.getString(R.string.on_agreement); // для отображения прогресса;

                        text1 = works.get(position).getRemained_label();
                        break;
                    case "progress_done_icon":
                        icon1 = R.drawable.progress_done_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.agreement_done);
                        text2 = "";
                        break;
                    case "progress_denied_icon":
                        icon1 = R.drawable.progress_denied_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.not_agreement);
                        text2 = "";
                        break;
                    case "progress_deleted_icon":
                        icon1 = R.drawable.progress_deleted_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.deleted);
                        text2 = "";
                        break;
                }
                break;
            case "approval":
                switch (work_state_icon) {
                    case "progress_mfa_icon":
                        icon2 = R.drawable.progress_mfa_icon;
                        icon1 = R.drawable.calendar_status_icon;
                        text2 = context.getString(R.string.on_approval);
                        text1 = works.get(position).getRemained_label();
                        break;
                    case "progress_done_icon":
                        icon1 = R.drawable.progress_done_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.approval_done);
                        text2 = "";
                        break;
                    case "progress_denied_icon":
                        icon1 = R.drawable.progress_denied_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.not_approved);
                        text2 = "";
                        break;
                    case "progress_deleted_icon":
                        icon1 = R.drawable.progress_deleted_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.deleted);
                        text2 = "";
                        break;
                }
                break;
            case "acquaintance":
                switch (work_state_icon) {
                    case "progress_mfa_icon":
                        icon2 = R.drawable.progress_mfa_icon;
                        icon1 = R.drawable.calendar_status_icon;
                        text2 = context.getString(R.string.on_acquaintance);
                        text1 = works.get(position).getRemained_label();
                        break;
                    case "progress_deleted_icon":
                        icon1 = R.drawable.progress_deleted_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.deleted);
                        text2 = "";
                        break;
                    case "progress_done_icon":
                        icon1 = R.drawable.progress_done_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.acquaintance_done);
                        text2 = "";
                        break;
                }
                break;
            case "registration":
                switch (work_state_icon) {
                    case "progress_mfa_icon":
                        icon1 = R.drawable.progress_mfa_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.on_registration);
                        text2 = "";
                        break;
                    case "progress_deleted_icon":
                        icon1 = R.drawable.progress_deleted_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.deleted);
                        text2 = "";
                        break;
                    case "progress_done_icon":
                        icon1 = R.drawable.progress_done_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.registered);
                        text2 = "";
                        break;
                    case "progress_denied_icon":
                        icon1 = R.drawable.progress_denied_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.not_registered);
                        text2 = "";
                        break;
                }
                break;
            case "result":
                icon1 = R.drawable.progress_result_icon;
                icon2 = android.R.color.transparent;
                text1 = context.getString(R.string.result);
                text2 = "";
                break;
            case "progress":
                switch (work_state_icon) {
                    case "progress_status_icon":
                        icon2 = R.drawable.progress_status_icon;
                        icon1 = R.drawable.calendar_status_icon;
                        text2 = works.get(position).getPercent() + "%";
                        text1 = works.get(position).getRemained_label();
                        break;
                    case "progress_done_icon":
                        icon1 = R.drawable.progress_done_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.finished);
                        text2 = "";
                        break;
                    case "progress_deleted_icon":
                        icon1 = R.drawable.progress_deleted_icon;
                        icon2 = android.R.color.transparent;
                        text1 = context.getString(R.string.deleted);
                        text2 = "";
                        break;
                }
                break;
            case "resolution-action":
                if (work_state_icon.equals("progress_resolution_icon")) {
                    icon2 = R.drawable.resolution_process_icon;
                    icon1 = R.drawable.calendar_status_icon;
                    text2 = context.getString(R.string.on_review);
                    text1 = works.get(position).getRemained_label();
                } else if (work_state_icon.equals("progress_resolution_done_icon") && works.get(position).getIs_resolution().equals("true")) {
                    icon1 = R.drawable.progress_done_icon;
                    icon2 = android.R.color.transparent;
                    text1 = context.getString(R.string.resolution);
                    text2 = "";
                } else if (work_state_icon.equals("progress_resolution_done_icon") && !works.get(position).getIs_resolution().equals("true")) {
                    icon1 = R.drawable.progress_deleted_icon;
                    icon2 = android.R.color.transparent;
                    text1 = context.getString(R.string.deleted);
                    text2 = "";
                } else if (work_state_icon.equals("progress_done_icon")) {
                    icon1 = R.drawable.progress_done_icon;
                    icon2 = android.R.color.transparent;
                    text1 = context.getString(R.string.finished);
                    text2 = "";
                }
                break;
            default:
                icon2 = R.drawable.progress_status_icon;
                icon1 = R.drawable.calendar_status_icon;
                text2 = "xD";
                text1 = "xD";
                break;
        }

        holder.calendarImage.setImageResource((Integer) icon1);
        holder.progressImage.setImageResource((Integer) icon2);
        holder.calendar.setText(text1);
        holder.calendar.setTypeface(Fonts.getMedium(context));
        holder.progress.setText(text2);
        holder.progress.setTypeface(Fonts.getMedium(context));

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

        if (works.get(position).getIs_expired().equals("true")) {
            holder.workText.setTextColor(context.getResources().getColor(R.color.expired_color_work));
        } else if (works.get(position).getIs_soon_expired().equals("true")) {
            holder.workText.setTextColor(context.getResources().getColor(R.color.soon_expired_color_work));
        } else {
            holder.workText.setTextColor(context.getResources().getColor(R.color.default_color_work));
        }

        // выставляем текст работы
        holder.workText.setText(works.get(position).getName());

        // проверяем есть ли какие-либо изменения, если есть - делаем текст работы жирным
        if (works.get(position).getIs_new().equals("true")) {
            holder.workText.setTypeface(Fonts.getMedium(context));
        } else {
            holder.workText.setTypeface(Fonts.getRoman(context));
        }

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

        return rowView;
    }

    static class ViewHolder {
        View priority;
        ImageView resolutionImage;
        ImageView calendarImage;
        ImageView progressImage;
        ImageView openChildWorks;
        TextView author;
        TextView calendar;
        TextView progress;
        TextView workText;
        ImageView gradient;
        RelativeLayout front;
    }
}
