package kz.arta.synergy.workflow.mobile.android.forms.general.components.repeat.weeks;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.common.requests.RestClient;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;
import kz.arta.synergy.workflow.mobile.android.forms.general.components.repeat.RepeatModel;
import kz.arta.synergy.workflow.mobile.android.forms.general.components.users.UsersChooserConst;

/**
 * Created by neox on 25.04.16.
 * Список для выбора недели
 */
public class WeekList {

    /**
     * Недели
     */
    private List<String> weeks = Arrays.asList("Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье");

    /**
     * Активити
     */
    private Activity activity;

    /**
     * Выделенные элементы
     */
    private String[] selected;

    /**
     * Инициализация
     */
    public void init() {
        (activity.findViewById(R.id.repeat_period_select_month_panel)).setVisibility(View.GONE);
        final List<RepeatModel> weekData = new ArrayList<>();
        for (String week : weeks) {
            RepeatModel model = new RepeatModel();
            model.setName(week);
            weekData.add(model);
        }
        if (selected != null && selected.length != 0) {
            for (String s : selected) {
                for (int i = 0; i < weekData.size(); i++) {
                    if (Double.parseDouble(s) == i + 1) {
                        weekData.get(i).setChecked(true);
                    }
                }
            }
        }
        ListView list = (ListView) activity.findViewById(R.id.repeat_period_week_list_view);
        list.setVisibility(View.VISIBLE);
        final ArrayAdapter<RepeatModel> adapter = new WeekAdapter(weekData);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                weekData.get(position).setChecked(!weekData.get(position).isChecked());
                adapter.notifyDataSetChanged();
            }
        });
        Button save = (Button) activity.findViewById(R.id.repeat_period_button_save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> result = new ArrayList<>();
                for (int i = 0; i < weekData.size(); i++) {
                    if (weekData.get(i).isChecked()) {
                        result.add(String.valueOf(i + 1) + ".0");
                    }
                }
                Intent intent = new Intent();
                intent.putExtra(UsersChooserConst.SELECTED_ITEMS, RestClient.getGson().toJson(result));
                activity.setResult(Activity.RESULT_OK, intent);
                activity.finish();
            }
        });
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void setSelected(String[] selected) {
        this.selected = selected;
    }

    public class WeekAdapter extends ArrayAdapter<RepeatModel> {

        /**
         * Список департаментов
         */
        private List<RepeatModel> weeks;

        /**
         * Конструктор адаптера департаментов
         *
         * @param weeks список недель
         */
        public WeekAdapter(List<RepeatModel> weeks) {
            super(activity, R.layout.week_item, weeks);
            this.weeks = weeks;
        }

        @Override
        public View getView(int position, View someView, ViewGroup arg2) {
            LayoutInflater inflater = LayoutInflater.from(activity);
            if (someView == null) {
                someView = inflater.inflate(R.layout.week_item, arg2, false);
            }
            TextView name = (TextView) someView.findViewById(R.id.week_item_name);
            name.setText(weeks.get(position).getName());
            name.setTypeface(Fonts.getRoman(activity));
            ImageView checker = (ImageView) someView.findViewById(R.id.week_item_icon);
            checker.setImageResource(weeks.get(position).isChecked() ? R.drawable.full_checkbox : R.drawable.checkbox);
            return someView;
        }
    }
}
