package kr.co.hanwha.hifive.screen.search;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.SearchTaskListAdapter;
import kr.co.hanwha.hifive.adapter.contract.SearchTaskListContract;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.view.HFSearchEditTextView;
import kr.co.hanwha.hifive.view.HFSearchProjectView;
import kr.co.hanwha.hifive.view.HFSearchRecentView;

/**
 * Created by sanghoonkwon on 2018. 1. 23..
 */

public class SearchActivity extends BaseActivity implements SearchContract.View {
    private HFSearchEditTextView mSearchEdit;

    private LinearLayout mLayoutRecent;
    private HFSearchRecentView mSearchRecentView;
    private HFSearchProjectView mSearchProjectView;
    private LinearLayout mLayoutResult;
    private LinearLayout mLayoutCount;
    private RecyclerView mRecyclerView;
    private LinearLayout mLayoutEmpty;

    private SearchTaskListAdapter mListAdapter;
    private SearchContract.Presenter mPresenter;

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_search;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return null;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return BaseAnimationType.RIGHT;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLayoutEmpty = (LinearLayout) findViewById(R.id.empty_linearLayout);

        mSearchEdit = (HFSearchEditTextView) findViewById(R.id.searchEditText);
        mSearchEdit.setOnSearchEditTextListener(onSearchEditTextListener);

        mLayoutRecent = (LinearLayout) findViewById(R.id.recent_linearLayout);

        mSearchRecentView = (HFSearchRecentView) findViewById(R.id.searchRecentView);
        mSearchRecentView.setOnSearchRecentListener(onSearchRecentListener);

        mSearchProjectView = (HFSearchProjectView) findViewById(R.id.searchProjectView);
        mSearchProjectView.setOnSearchProjectListener(onSearchProjectListener);

        mLayoutResult = (LinearLayout) findViewById(R.id.result_linearLayout);

        mListAdapter = new SearchTaskListAdapter();
        mListAdapter.setOnSearchListListener(onSearchListListener);

        mLayoutCount = (LinearLayout) findViewById(R.id.count_linearLayout);

        mRecyclerView = (RecyclerView) findViewById(R.id.quest_recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mListAdapter);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        mRecyclerView.addOnScrollListener(onScrollListener);

        findViewById(R.id.back_imageButton).setOnClickListener(onClickListener);

        mPresenter = new SearchPresenter(this, mListAdapter, mListAdapter);
        mPresenter.initRecentKeyword();

        String searchWord = getIntent().getStringExtra(HFActivityUtil.INTENT_SEARCH_WORD);
        if (TextUtils.isEmpty(searchWord) == false) {
            mSearchEdit.setText(searchWord);
            mPresenter.doSearch(searchWord);
        } else {
            mLayoutRecent.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mPresenter.detachView();
    }

    @Override
    public void setSearchResult(int count) {
        mLayoutRecent.setVisibility(View.GONE);
        mLayoutResult.setVisibility(View.VISIBLE);

//        ((TextView) findViewById(R.id.taskCount_textView)).setText(String.format(getString(R.string.search_result_count), count));
        ((TextView) findViewById(R.id.taskCount_textView)).setText(Integer.toString(count));
        if (count > 0) {
            mLayoutCount.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.VISIBLE);
            mLayoutEmpty.setVisibility(View.GONE);
        } else {
            String keyword = mSearchEdit.getText();
            TextView textViewEmpty = (TextView) findViewById(R.id.empty_textView);
            textViewEmpty.setText(String.format(getString(R.string.search_result_empty), keyword));
            mLayoutCount.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.GONE);
            mLayoutEmpty.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void setRecentKeyword(ArrayList<String> list) {
        mSearchRecentView.setRecentKeyword(list);

    }

    @Override
    public void setProjectKeyword(ArrayList<String> list) {
        mSearchProjectView.setProjectKeyword(list);
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.back_imageButton:
                    finish();
                    break;
            }
        }
    };

    /**************************************************
     * SearchTaskListContract.OnSearchListListener
     **************************************************/
    SearchTaskListContract.OnSearchListListener onSearchListListener = new SearchTaskListContract.OnSearchListListener() {
        @Override
        public void onTaskClicked(@NonNull TaskListData data) {
            HFActivityUtil.startTaskDetailActivity(SearchActivity.this, data);
        }
    };

    /**************************************************
     * SearchRecentView.OnSearchRecentListener
     **************************************************/
    HFSearchRecentView.OnSearchRecentListener onSearchRecentListener = new HFSearchRecentView.OnSearchRecentListener() {
        @Override
        public void keywordClicked(@NonNull String keyword) {
            mSearchEdit.setText(keyword);
            mSearchEdit.hideKeyboard();
            mPresenter.doSearch(keyword);
        }

        @Override
        public void keywordDeleted() {
            mPresenter.initRecentKeyword();
        }
    };
    /**************************************************
     * SearchProjectView.OnSearchProjectListener
     **************************************************/
    HFSearchProjectView.OnSearchProjectListener onSearchProjectListener = new HFSearchProjectView.OnSearchProjectListener() {
        @Override
        public void keywordClicked(@NonNull String keyword) {
            mSearchEdit.setText(keyword);
            mSearchEdit.hideKeyboard();
            mPresenter.doSearch(keyword);
        }
    };

    /**************************************************
     * SearchEditTextView.OnSearchEditTextListener
     **************************************************/
    HFSearchEditTextView.OnSearchEditTextListener onSearchEditTextListener = new HFSearchEditTextView.OnSearchEditTextListener() {
        @Override
        public void onImeActionSearch(@NonNull HFSearchEditTextView v) {
            String keyword = v.getText();
            if (TextUtils.isEmpty(keyword) == false) {
//                GWInfoUtil.setRecentKeyword(SearchActivity.this, keyword);
//                mPresenter.initRecentKeyword();
                mPresenter.doSearch(keyword);
            }
        }

        @Override
        public void onValueChanged(@NonNull HFSearchEditTextView v) {

        }
    };
}
