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

import android.animation.Animator;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.AddTaskSelectMemberListAdapter;
import kr.co.hanwha.hifive.adapter.AddTaskSelectedMemberPagerAdapter;
import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectMemberListContract;
import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectedMemberPagerContract;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.data.model.ProjectInfoData;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.screen.task.contract.AddTaskSelectMemberContract;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.view.HFSearchMemberEditTextView;
import kr.co.hanwha.hifive.view.HFTabView;

/**
 * Add TASK  : 멤버 선택 View
 *
 * @see AddTaskSelectMemberPresenter
 * Created by sanghoonkwon on 2018. 1. 18..
 */

public class AddTaskSelectMemberActivity extends BaseActivity implements AddTaskSelectMemberContract.View {
    private ViewPager mViewPager;
    private LinearLayout mLayoutContainer;
    private HFSearchMemberEditTextView mSearchEditTextView;
    private RecyclerView mRecyclerView;
    private View mViewMargin;

    private AddTaskSelectMemberListAdapter mListAdapter;
    private AddTaskSelectedMemberPagerAdapter mPagerAdapter;

    private boolean isVisiblePager = false;

    private AddTaskSelectMemberContract.Presenter mPresenter;

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

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

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

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

        mPagerAdapter = new AddTaskSelectedMemberPagerAdapter(this, onMemberPagerListener);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setAdapter(mPagerAdapter);

        mSearchEditTextView = (HFSearchMemberEditTextView) findViewById(R.id.searchMemberEditTextView);
        mSearchEditTextView.setOnSearchEditTextListener(onSearchEditTextListener);

        mLayoutContainer = (LinearLayout) findViewById(R.id.container_linearLayout);
        mViewMargin = (View) findViewById(R.id.margin_view);

        mListAdapter = new AddTaskSelectMemberListAdapter(onSelectMemberListListener);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mListAdapter);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        findViewById(R.id.confirm_textView).setOnClickListener(onClickListener);
        findViewById(R.id.close_imageButton).setOnClickListener(onClickListener);

        mPresenter = new AddTaskSelectMemberPresenter(this, mListAdapter, mListAdapter, mPagerAdapter, mPagerAdapter);
        mPresenter.initData();

        mPresenter.refreshSelectedMember();

        final TextView textViewConfirm = (TextView) findViewById(R.id.confirm_textView);
        textViewConfirm.postDelayed(new Runnable() {
            @Override
            public void run() {
                textViewConfirm.setVisibility(View.VISIBLE);
                Animation anim = new AlphaAnimation(0.0f, 1.0f);
                anim.setDuration(300);
                textViewConfirm.startAnimation(anim);
            }
        }, 300);

    }

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

        mPresenter.detachView();
    }

    @Override
    public void setSelectedMemberVisible(boolean visible) {
        int height = HFCommonUtil.getPixelFromDimension(this, R.dimen.add_task_member_pager_height);
        if (visible) {
            if (isVisiblePager) return;
            isVisiblePager = true;
            mLayoutContainer.setTranslationY(0);
            mLayoutContainer.animate()
                    .translationY(height)
                    .setDuration(300)
                    .setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animator) {
                            mViewMargin.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onAnimationCancel(Animator animator) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animator) {

                        }
                    })
                    .start();

//            mRecyclerView.scrollBy(0, height);
        } else {
            if (isVisiblePager == false) return;
            isVisiblePager = false;
            mViewMargin.setVisibility(View.GONE);
            mLayoutContainer.setTranslationY(height);
            mLayoutContainer.animate()
                    .translationY(0)
                    .setDuration(300)
                    .setListener(null)
                    .start();

//            mRecyclerView.scrollBy(0, -height);
        }
    }

    @Override
    public void onBackPressed() {
        finishAnimation();
//        super.onBackPressed();
    }

    private void finishAnimation() {
        final TextView textViewConfirm = (TextView) findViewById(R.id.confirm_textView);
        textViewConfirm.setVisibility(View.VISIBLE);
        Animation anim = new AlphaAnimation(1.0f, 0.0f);
        anim.setDuration(200);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                textViewConfirm.setVisibility(View.INVISIBLE);
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        textViewConfirm.startAnimation(anim);
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.confirm_textView:
                    mPresenter.doCompleted();
                    break;
                case R.id.close_imageButton:
                    finishAnimation();
                    break;
            }

        }
    };

    /**************************************************
     * Support methods
     **************************************************/
    public void setCompleteCount(int count) {
        ((TextView) findViewById(R.id.count_textView)).setText(Integer.toString(count));

        findViewById(R.id.confirm_textView).setEnabled(count > 0);

        findViewById(R.id.count_textView).setVisibility(count > 0 ? View.VISIBLE : View.GONE);
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    HFSearchMemberEditTextView.OnSearchEditTextListener onSearchEditTextListener = new HFSearchMemberEditTextView.OnSearchEditTextListener() {
        @Override
        public void onImeActionSearch(@NonNull HFSearchMemberEditTextView v) {

        }

        @Override
        public void onValueChanged(@NonNull HFSearchMemberEditTextView v) {
            String keyword = v.getText();
            mListAdapter.getFilter().filter(keyword);
        }
    };

    /**************************************************
     * SelectMemberPagerContract.OnMemberPagerListener
     **************************************************/
    AddTaskSelectedMemberPagerContract.OnMemberPagerListener onMemberPagerListener = new AddTaskSelectedMemberPagerContract.OnMemberPagerListener() {
        @Override
        public void onDeleteClick(@NonNull MemberInfoData data, int index) {
            mPresenter.updateObjectSelect(data, -1, false);
        }
    };

    /**************************************************
     * AddTaskSelectMemberListContract.OnSelectMemberListListener
     **************************************************/
    AddTaskSelectMemberListContract.OnSelectMemberListListener onSelectMemberListListener = new AddTaskSelectMemberListContract.OnSelectMemberListListener() {
        @Override
        public void onItemClicked(@NonNull MemberInfoData data, int index) {
            mPresenter.updateObjectSelect(data, index, !data.isSelected());
        }
    };
}
