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

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.common.models.LogSyncItem;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;

/**
 * Created by neox on 23.07.14.
 * Адаптер для списка логов
 */
public class AdapterLogs extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader;
    private HashMap<String, List<LogSyncItem>> _listDataChild;

    public AdapterLogs(Context context, List<String> listDataHeader, HashMap<String, List<LogSyncItem>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    public Object getMessage(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon).getMessage();
    }

    public Object getData(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon).getData();
    }

    public Object getImage(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon).getType();
    }


    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        final String message1 = (String) getMessage(groupPosition, childPosition);
        final String data1 = (String) getData(groupPosition, childPosition);
        final String type1 = (String) getImage(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item_logs, null);
        }

        TextView message = (TextView) convertView.findViewById(R.id.sync_message);
        message.setText(message1);
        message.setTypeface(Fonts.getMedium(_context));

        TextView data = (TextView) convertView.findViewById(R.id.sync_data);
        data.setTypeface(Fonts.getMedium(_context));
        data.setText(data1);

        ImageView icon = (ImageView) convertView.findViewById(R.id.sync_log_icon);
        if (type1.equals("1")) {
            icon.setImageResource(R.drawable.succefull_sync);
        } else if (type1.equals("2")) {
            icon.setImageResource(R.drawable.error_sync);
        } else if (type1.equals("0")) {
            icon.setImageResource(android.R.drawable.screen_background_light_transparent);
        } else if (type1.equals("3")) {
            icon.setImageResource(R.drawable.sync_yellow_succesfull);
        }

        if (isLastChild) {
            convertView.setBackgroundResource(R.drawable.background_bottom_layout);
        } else {
            convertView.setBackgroundResource(R.drawable.center_sync_list);
        }

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int i, int i2) {
        return null;
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group_log, null);
        }

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        lblListHeader.setTypeface(Fonts.getMedium(_context));

        ImageView icon = (ImageView) convertView.findViewById(R.id.list_group_icon);

        RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.background_group);

        if (isExpanded) {
            icon.setBackgroundResource(R.drawable.up_log_icon);
            relativeLayout.setBackgroundResource(R.drawable.background_parent_opened_log);
        } else {
            icon.setBackgroundResource(R.drawable.down_log_icon);
            relativeLayout.setBackgroundResource(R.drawable.background_parent_log);
        }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}
