package kz.arta.synergy.workflow.mobile.android.forms.general.components.users.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.pnikosis.materialishprogress.ProgressWheel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.common.ActivityConst;
import kz.arta.synergy.workflow.mobile.android.common.components.SearchEditText;
import kz.arta.synergy.workflow.mobile.android.common.models.Person;
import kz.arta.synergy.workflow.mobile.android.common.requests.RestClient;
import kz.arta.synergy.workflow.mobile.android.common.requests.SendGetRequest;
import kz.arta.synergy.workflow.mobile.android.common.utils.Const;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;
import kz.arta.synergy.workflow.mobile.android.common.utils.FragmentConst;
import kz.arta.synergy.workflow.mobile.android.common.utils.HorizontalListView;
import kz.arta.synergy.workflow.mobile.android.common.utils.Utils;
import kz.arta.synergy.workflow.mobile.android.files.adapters.AdapterPath;
import kz.arta.synergy.workflow.mobile.android.files.models.Path;
import kz.arta.synergy.workflow.mobile.android.forms.general.components.users.UsersChooserConst;
import kz.arta.synergy.workflow.mobile.android.forms.general.components.users.adapters.DepartmentsChooserAdapter;
import kz.arta.synergy.workflow.mobile.android.forms.general.components.users.model.DepartmentModel;
import kz.arta.synergy.workflow.mobile.android.forms.general.components.users.model.UserModel;
import kz.arta.synergy.workflow.mobile.android.forms.general.components.users.navigator.UsersChooserNavigatorItem;

/**
 * Created by neox on 05.04.16.
 * Департамент чузер
 */
public class DepartmentsChooserActivity extends Activity {

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

    /**
     * Прогрессбар
     */
    private ProgressWheel progressBar;

    /**
     * Авторизированный пользователь
     */
    private Person person;

    /**
     * Боковая навигация
     */
    private SlidingMenu slidingMenu;

    /**
     * Контейнер с навигатором
     */
    private LinearLayout navigatorLayout;

    /**
     * Адаптер для списка департаментов
     */
    private DepartmentsChooserAdapter departmentsAdapter;

    /**
     * Департаменты в списке
     */
    private List<DepartmentModel> departmentsArray;

    /**
     * Выбранные департаменты
     */
    private List<DepartmentModel> selectedDepartments;

    /**
     * Данные компонента "Путь"
     */
    private List<Path> pathData;

    /**
     * Адаптер компонента "Путь"
     */
    private BaseAdapter pathAdapter;

    /**
     * Компонент "Путь"
     */
    private HorizontalListView pathList;

    /**
     * Компонент для поиска
     */
    private SearchEditText searchEditText;

    /**
     * Идентификатор текущего департамента
     */
    private String departmentID;

    /**
     * Используется ли мультивыбор
     */
    private boolean multiSelect;

    /**
     * Фильтрация по userID
     */
    private String filterUserID;

    /**
     * Фильтрация по departmentID
     */
    private String filterDepartmentID;

    /**
     * Фильтрация по positionID
     */
    private String filterPositionID;

    /**
     * С какой записи загружать пользователей
     */
    private int startRecord = 0;

    /**
     * Загружается ли список в данный момент
     */
    private boolean isLoading = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.users_chooser_activity);
        super.onCreate(savedInstanceState);

        activity = this;

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        Intent intent = getIntent();
        person = (Person) intent.getSerializableExtra(ActivityConst.PERSON);
        multiSelect = intent.getBooleanExtra(UsersChooserConst.MULTI_SELECT, false);
        filterUserID = intent.getStringExtra(UsersChooserConst.FILTER_USER_ID);
        filterDepartmentID = intent.getStringExtra(UsersChooserConst.FILTER_DEPARTMENT_ID);
        filterPositionID = intent.getStringExtra(UsersChooserConst.FILTER_POSITION_ID);
        selectedDepartments = RestClient.getGson().fromJson(intent.getStringExtra(UsersChooserConst.SELECTED_ITEMS),
                new TypeToken<List<DepartmentModel>>() {
                }.getType());

        if (selectedDepartments != null) {
            for (DepartmentModel user : selectedDepartments) {
                user.setChecked(true);
            }
        }

        init();
    }

    /**
     * Инициализация
     */
    public void init() {

        ((TextView) findViewById(R.id.users_chooser_action_bar_title)).setText(R.string.departments);

        progressBar = (ProgressWheel) findViewById(R.id.user_chooser_progress_bar);

        // кнопка "Навигатор"
        ImageView showNavigator = (ImageView) findViewById(R.id.user_chooser_icon_show_navigator);
        showNavigator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.toggle();
            }
        });

        // компонент "Путь"
        pathData = new ArrayList<>();
        pathAdapter = new AdapterPath(activity, pathData);
        pathList = (HorizontalListView) findViewById(R.id.list_view_path_user_chooser);
        pathList.setAdapter(pathAdapter);
        pathList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // все элементы отмечаем как "невыделенные"
                for (Path path : pathData) {
                    path.setSelected(false);
                }
                // говорим элемент, по которому происходит тап - будет выделен
                pathData.get(i).setSelected(true);
                searchListItem(navigatorLayout, pathData.get(i).getNodeID());
            }
        });

        // компонент для поиска
        searchEditText = (SearchEditText) findViewById(R.id.search_user_edit_text);
        searchEditText.setHint(getString(R.string.search_departments));
        searchEditText.setListener(new SearchEditText.SearchTextListener() {
            @Override
            public void onTextChanged(String text) {
                startRecord = 0;
                getDepartments(departmentID, text, true);
            }
        });

        if (selectedDepartments == null) {
            selectedDepartments = new ArrayList<>();
        }

        departmentsArray = new ArrayList<>();
        departmentsAdapter = new DepartmentsChooserAdapter(this, departmentsArray, multiSelect);

        // основной список
        ListView users = (ListView) findViewById(R.id.users_list);
        users.setAdapter(departmentsAdapter);
        users.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (multiSelect) {
                    if (!departmentsArray.get(i).isChecked()) {
                        selectedDepartments.add(departmentsArray.get(i));
                    } else {
                        String departmentID = departmentsArray.get(i).getDepartmentID();
                        int foundIndex = -1;
                        for (int k = 0; k < selectedDepartments.size(); k++) {
                            if (departmentID.equals(selectedDepartments.get(k).getDepartmentID())) {
                                foundIndex = k;
                            }
                        }
                        selectedDepartments.remove(foundIndex);
                    }
                    departmentsArray.get(i).setChecked(!departmentsArray.get(i).isChecked());
                } else {
                    for (DepartmentModel department : departmentsArray) {
                        department.setChecked(false);
                    }
                    departmentsArray.get(i).setChecked(true);
                    selectedDepartments.clear();
                    selectedDepartments.add(departmentsArray.get(i));
                }
                departmentsAdapter.notifyDataSetChanged();
            }
        });

        users.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (totalItemCount >= FragmentConst.DEFAULT_COUNT_WORKS) {
                    final int lastItem = firstVisibleItem + visibleItemCount;
                    if (lastItem == totalItemCount) {
                        if (!isLoading) {
                            isLoading = true;
                            startRecord++;
                            getDepartments(departmentID, "", false);
                        }
                    }
                }
            }

            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
            }
        });

        // навигатор
        View view = getLayoutTree();
        slidingMenu = new SlidingMenu(getApplicationContext());
        slidingMenu.setMode(SlidingMenu.RIGHT);
        slidingMenu.attachToActivity(activity, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(view);
        slidingMenu.setBehindWidth(Utils.convertDpToPixel(Const.SLIDING_MENU_WIDTH, activity));

        // главный контейнер фильтров
        final SwipeRefreshLayout filtersScroll = (SwipeRefreshLayout) view.findViewById(R.id.refresh_filters);
        filtersScroll.setEnabled(false);

        navigatorLayout = (LinearLayout) view.findViewById(R.id.filters_list);

        getSelectedUsers(navigatorLayout);

        getDepartments(navigatorLayout);

        // кнопка "Готово"
        Button save = (Button) findViewById(R.id.user_chooser_activity_save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(UsersChooserConst.SELECTED_ITEMS, RestClient.getGson().toJson(selectedDepartments));
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    /**
     * Метод возвращает view навигатора
     *
     * @return view
     */
    private View getLayoutTree() {
        LayoutInflater ltInflater = getLayoutInflater();
        View view = ltInflater.inflate(R.layout.fragment_filters, null, false);
        (view.findViewById(R.id.sync_button_title)).setVisibility(View.GONE);
        (view.findViewById(R.id.sync_icon)).setVisibility(View.GONE);
        (view.findViewById(R.id.sync_button)).setVisibility(View.GONE);
        (view.findViewById(R.id.icon_settings)).setVisibility(View.GONE);
        ((TextView) view.findViewById(R.id.title_action_bar_filters)).setTypeface(Fonts.getMedium(activity));
        return view;
    }

    /**
     * Метод вызывается при нажатии на элемент пути
     *
     * @param layout контейнер
     * @param uuid   идентификатор элемента
     */
    private void searchListItem(LinearLayout layout, String uuid) {
        for (int i = 0; i < layout.getChildCount(); i++) {
            if (layout.getChildAt(i) instanceof UsersChooserNavigatorItem) {
                UsersChooserNavigatorItem node = (UsersChooserNavigatorItem) layout.getChildAt(i);
                if (node.getDepartmentID() != null && node.getDepartmentID().equals(uuid)) {
                    node.setSelected();
                    node.tapItem();
                    return;
                }
                searchListItem(node, uuid);
            }
        }
    }

    /**
     * Метод заполняет данными компонент "путь"
     *
     * @param pathItems элементы пути
     */
    public void initPath(List<UserModel> pathItems) {
        // очистка пути
        pathData.clear();
        for (int i = 0; i < pathItems.size(); i++) {
            Path path = new Path();
            path.setName(pathItems.get(i).getDepartmentName());
            path.setNodeID(pathItems.get(i).getUuid());
            pathData.add(path);
            if (i == pathItems.size() - 1) {
                path.setSelected(true);
            }
        }
        // обновляем компонент
        pathAdapter.notifyDataSetChanged();
        // скроллим список до конца
        pathList.scrollTo(Integer.MAX_VALUE);
    }

    /**
     * Метод загружает в список департаменты по указанному departmentID
     *
     * @param departmentID идентификатор департамента
     * @param searchText   строка поиска
     */
    public void getDepartments(final String departmentID, String searchText, final boolean clearList) {
        this.departmentID = departmentID;
        new SendGetRequest("rest/api/userchooser/search_dept" +
                "?departmentID=" + departmentID +
                (filterUserID != null && !filterUserID.equals("null") ? "&filterUserID=" + filterUserID : "") +
                (filterPositionID != null && !filterPositionID.equals("null") ? "&filterPositionID=" + filterPositionID : "") +
                (filterDepartmentID != null && !filterDepartmentID.equals("null") ? "&filterDepartmentID=" + filterDepartmentID : "") +
                "&showChildDepartments=true" +
                "&pageNumber=" + startRecord +
                "&countInPart=" + FragmentConst.DEFAULT_COUNT_WORKS +
                "&search=" + searchText, person) {
            @Override
            protected void onPreExecute() {
                progressBar.setVisibility(View.VISIBLE);
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(String result) {
                progressBar.setVisibility(View.GONE);
                if (result == null) {
                    return;
                }
                if (clearList) {
                    departmentsArray.clear();
                }
                DepartmentModel[] departments = RestClient.getGson().fromJson(result, DepartmentModel[].class);
                isLoading = departments.length == 0;
                Collections.addAll(departmentsArray, departments);
                for (int i = 0; i < departmentsArray.size(); i++) {
                    for (int j = 0; j < selectedDepartments.size(); j++) {
                        String departmentID = departmentsArray.get(i).getDepartmentID();
                        String selectedDepartmentID = selectedDepartments.get(j).getDepartmentID();
                        if (departmentID.equals(selectedDepartmentID)) {
                            departmentsArray.get(i).setChecked(true);
                        }
                    }
                }
                departmentsAdapter.notifyDataSetChanged();
            }
        }.execute();
    }

    /**
     * Метод добавляет плашку выбранных пользователей
     *
     * @param layout контейнер
     */
    private void getSelectedUsers(final LinearLayout layout) {

        UserModel userModel = new UserModel();
        userModel.setDepartmentName(getString(R.string.selected));

        List<UserModel> parents = new ArrayList<>();
        parents.add(userModel);

        UsersChooserNavigatorItem item = new UsersChooserNavigatorItem(activity);
        item.setPerson(person);
        item.setName(getString(R.string.selected));
        item.setLevel(1);
        item.setType(UsersChooserConst.SELECTED_USERS);
        item.setComponentType(UsersChooserConst.DEPARTMENT_CHOOSER);
        item.setHideIcon(true);
        item.setParents(parents);
        item.add();

        layout.addView(item);
    }

    /**
     * Метод добавляет плашку департаментов
     *
     * @param layout контейнер
     */
    public void getDepartments(final LinearLayout layout) {
        new SendGetRequest("rest/api/userchooser/search_dept?departmentID=" +
                "32b36e0c-4621-4c96-99e1-3826284c363e&showChildDepartments=false", person) {
            @Override
            protected void onPostExecute(String result) {
                if (result == null) {
                    return;
                }
                try {

                    DepartmentModel[] departments = RestClient.getGson().fromJson(result, DepartmentModel[].class);

                    UserModel userModel = new UserModel();
                    userModel.setDepartmentName(departments[0].getDepartmentName());
                    userModel.setUuid(departments[0].getDepartmentID());

                    List<UserModel> parents = new ArrayList<>();
                    parents.add(userModel);

                    UsersChooserNavigatorItem item = new UsersChooserNavigatorItem(activity);
                    item.setPerson(person);
                    item.setName(departments[0].getDepartmentName());
                    item.setLevel(1);
                    item.setType(UsersChooserConst.DEPARTMENTS);
                    item.setComponentType(UsersChooserConst.DEPARTMENT_CHOOSER);
                    item.setDepartmentID(departments[0].getDepartmentID());
                    item.setParents(parents);
                    item.add();
                    item.setSelected();
                    item.expand();
                    item.tapItem();

                    layout.addView(item);

                } catch (Exception exc) {
                    // handling error...
                }
            }
        }.execute();
    }

    @Override
    public void onBackPressed() {
        if (slidingMenu.isMenuShowing()) {
            hideMenu();
        } else {
            super.onBackPressed();
        }
    }

    /**
     * Метод добавляет в список выбранных пользователей
     */
    public void getSelected() {
        try {
            departmentsArray.clear();
            for (DepartmentModel department : selectedDepartments) {
                departmentsArray.add(department);
            }
            departmentsAdapter.notifyDataSetChanged();
        } catch (Exception exc) {
            // handling error...
        }
    }

    public void hideMenu() {
        slidingMenu.toggle();
    }
}
