package kr.co.hanwha.hifive.screen.main.setting.myhome;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.MainHomeRequestListAdapter;
import kr.co.hanwha.hifive.adapter.MainMyHomeTodoListAdapter;
import kr.co.hanwha.hifive.adapter.SettingMyhomeOkrListAdapter;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.CampaignData;
import kr.co.hanwha.hifive.data.model.ProfileData;
import kr.co.hanwha.hifive.data.model.ProjectInfoData;
import kr.co.hanwha.hifive.data.model.TaskData;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.screen.main.home.contract.MainHomeContract;
import kr.co.hanwha.hifive.screen.main.setting.myhome.contract.MyHomeContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.view.HFMyHomeHeaderView;
import kr.co.hanwha.hifive.view.HFMyHomeProfileUnitView;
import kr.co.hanwha.hifive.view.HFMyHomeTabUnderlineView;

public class MyHomeActivity extends BaseActivity implements MyHomeContract.View {

    private ViewPager mViewPager;
    private View mViewOkr;
    private View mViewTodo;
    private View mViewRequest;
    private View mViewProfile;
    private RecyclerView mRecyclerViewTodo;
    private RecyclerView mRecyclerViewOkr;
    private RecyclerView mRecyclerViewRequest;
    private SwipeRefreshLayout mRefreshLayoutOkr;
    private SwipeRefreshLayout mRefreshLayoutTodo;
    private SwipeRefreshLayout mRefreshLayoutRequest;
    private SwipeRefreshLayout mRefreshLayoutProfile;
    private SettingMyhomeOkrListAdapter mAdpaterOkr;
    private MainMyHomeTodoListAdapter mAdapterTodo;
    private MainHomeRequestListAdapter mAdapterRequest;
    private HFMyHomeTabUnderlineView mMyHomeTabUnderlineView;
    private HFMyHomeHeaderView mMyHomeHeaderView;
    private LinearLayout mLinearLayoutContainer;
    private TextView mTextViewCompany;
    private TextView mTextViewPosition;
    private TextView mTextViewCellphone;
    private TextView mTextViewOkrEmpty;

    private MyHomePresenter mPresenter;

    public static final int INDEX_OKR = 0;
    public static final int INDEX_TODO = 1;
    public static final int INDEX_REQUEST = 2;
    public static final int INDEX_PROFILE = 3;

    private int mTabIndex = 0;
    private int mMemberId = 0;

    private ProfileData mProfileData;

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

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return BaseToolbarType.ONLY_BACK;
    }

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

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

        if (getIntent() != null) mMemberId = getIntent().getIntExtra(HFActivityUtil.INTENT_MEMBER_ID, 0);

        initLayout();

        initPresenter();

        mPresenter.initDataOkr(mMemberId, true);
        mPresenter.initDataProfile(mMemberId, true);

        if (mTabIndex >= 0 && mTabIndex <= 3)
            mViewPager.setCurrentItem(mTabIndex);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.initDataTodo(mMemberId, true);
        mPresenter.initDataRequest(mMemberId, true);
    }

    private void initLayout() {
        setToolbarBackgroundColor(R.color.hf_grey19);

//        String memberName = getIntent().getStringExtra(HFActivityUtil.INTENT_MEMBER_NAME);
//        String loginId = getIntent().getStringExtra(HFActivityUtil.INTENT_MEMBER_LOGIN_ID);

        mMyHomeHeaderView = (HFMyHomeHeaderView) findViewById(R.id.myHomeHeaderView);
//        mMyHomeHeaderView.setMemberInfo(memberName, loginId);
        mMyHomeHeaderView.setOnMyHomeClickListener(onMyHomeClickListener);

        mViewOkr = LayoutInflater.from(this).inflate(R.layout.fragment_setting_my_home_okr, null);
        mViewTodo = LayoutInflater.from(this).inflate(R.layout.fragment_main_home_sub, null);
        mViewRequest = LayoutInflater.from(this).inflate(R.layout.fragment_main_home_sub, null);
        mViewProfile = LayoutInflater.from(this).inflate(R.layout.fragment_setting_my_home_profile, null);

        mMyHomeTabUnderlineView = (HFMyHomeTabUnderlineView) findViewById(R.id.myHomeTabUnderlineView);
        mMyHomeTabUnderlineView.setOnMyHomeTabListener(new HFMyHomeTabUnderlineView.OnMyHomeTabListener() {
            @Override
            public void onTabSelected(int position) {
                mViewPager.setCurrentItem(position);
            }
        });

        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        MyHomePagerAdapter pagerAdapter = new MyHomePagerAdapter();
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.addOnPageChangeListener(onPageChangeListener);
        mViewPager.setOffscreenPageLimit(3);

        // OKR
        mRefreshLayoutOkr = (SwipeRefreshLayout) mViewOkr.findViewById(R.id.swipeRefreshLayout);
        mRefreshLayoutOkr.setOnRefreshListener(onOkrRefreshListener);

        mAdpaterOkr = new SettingMyhomeOkrListAdapter(this, onMyHomeOkrListListener);
        mRecyclerViewOkr = (RecyclerView) mViewOkr.findViewById(R.id.recyclerView);
        mRecyclerViewOkr.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerViewOkr.setAdapter(mAdpaterOkr);

        mTextViewOkrEmpty = (TextView) mViewOkr.findViewById(R.id.empty_textView);

        // To-Do
        mRefreshLayoutTodo = (SwipeRefreshLayout) mViewTodo.findViewById(R.id.swipeRefreshLayout);
        mRefreshLayoutTodo.setOnRefreshListener(onTodoRefreshListener);

//        mAdapterTodo = new SettingMyHomeTodoListAdapter(this, onMyHomeTodoListListener);
        mAdapterTodo = new MainMyHomeTodoListAdapter(this, onMainHomeListListener);
        mAdapterTodo.setFromMain(false);
        mRecyclerViewTodo = (RecyclerView) mViewTodo.findViewById(R.id.recyclerView);
        mRecyclerViewTodo.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerViewTodo.setAdapter(mAdapterTodo);

        //Request
        mRefreshLayoutRequest = (SwipeRefreshLayout) mViewRequest.findViewById(R.id.swipeRefreshLayout);
        mRefreshLayoutRequest.setOnRefreshListener(onRequestRefreshListener);

        mAdapterRequest = new MainHomeRequestListAdapter(this, onMainHomeListListener);
        mAdapterRequest.setFromMain(false);
        mRecyclerViewRequest = (RecyclerView) mViewRequest.findViewById(R.id.recyclerView);
        mRecyclerViewRequest.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerViewRequest.setAdapter(mAdapterRequest);

        // Profile
        mRefreshLayoutProfile = (SwipeRefreshLayout) mViewProfile.findViewById(R.id.swipeRefreshLayout);
        mRefreshLayoutProfile.setOnRefreshListener(onProfileRefreshListener);
        mLinearLayoutContainer = (LinearLayout) mViewProfile.findViewById(R.id.container_linearLayout);
        mTextViewCompany = (TextView) mViewProfile.findViewById(R.id.company_textView);
        mTextViewPosition = (TextView) mViewProfile.findViewById(R.id.position_textView);
        mTextViewCellphone = (TextView) mViewProfile.findViewById(R.id.cellPhone_textView);

    }

    private void initPresenter() {
        mPresenter = new MyHomePresenter(this, mAdapterTodo, mAdapterTodo, mAdpaterOkr, mAdpaterOkr, mAdapterRequest, mAdapterRequest);
    }

    /*@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == HFActivityUtil.REQUEST_ADD_TASK || requestCode == HFActivityUtil.REQUEST_TASK_DETAIL) {
                mPresenter.initDataTodo(true);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }*/

    /**************************************************
     * ViewPager.OnPageChangeListener
     **************************************************/
    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mMyHomeTabUnderlineView.onPageScrolled(position, positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            mMyHomeTabUnderlineView.setCurrentItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    /**************************************************
     * MyHomeContract.OnSettingMyHomeOkrListListener
     **************************************************/
    MyHomeContract.OnSettingMyHomeOkrListListener onMyHomeOkrListListener = new MyHomeContract.OnSettingMyHomeOkrListListener() {
        @Override
        public void onPlusClicked(@NonNull CampaignData campaignData) {
            HFActivityUtil.startAddTaskFromMyHomeActivity(MyHomeActivity.this, mProfileData, campaignData);
        }

        @Override
        public void showEmptyView(boolean isShow) {
            mTextViewOkrEmpty.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    };

    /**************************************************
     * MyHomeContract.OnSettingMyHomeTodoListListener
     **************************************************/
    MyHomeContract.OnSettingMyHomeTodoListListener onMyHomeTodoListListener = new MyHomeContract.OnSettingMyHomeTodoListListener() {
        @Override
        public void onItemClicked(@NonNull TaskData data) {
            HFActivityUtil.startTaskDetailActivity(MyHomeActivity.this, data);
        }
    };

    @Override
    public void refreshData() {

    }

    @Override
    public void stopRefreshOkr() {
        mRefreshLayoutOkr.setRefreshing(false);
    }

    @Override
    public void stopRefreshTodo() {
        mRefreshLayoutTodo.setRefreshing(false);
    }

    @Override
    public void stopRefreshRequest() {
        mRefreshLayoutRequest.setRefreshing(false);
    }

    @Override
    public void stopRefreshProfile() {
        mRefreshLayoutProfile.setRefreshing(false);
    }

    @Override
    public void setTabIndex(int index) {
        mTabIndex = index;
    }

    @Override
    public void setProfileUnit(List<ProjectInfoData> projects) {
        if (mLinearLayoutContainer.getChildCount() > 0)
            mLinearLayoutContainer.removeAllViews();

        for (final ProjectInfoData pid : projects) {
            HFMyHomeProfileUnitView view = new HFMyHomeProfileUnitView(this);
            view.setProjectInfo(pid.getProject_avg_achieve_rate(), pid.getProject_name());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    HFActivityUtil.startDashboardProjectActivity(MyHomeActivity.this, pid.getProject_id());
                }
            });
            mLinearLayoutContainer.addView(view);
        }
    }

    @Override
    public void setProfile(@NonNull ProfileData profileData) {
        mProfileData = profileData;
        mMyHomeHeaderView.setMemberInfo(profileData.getMember_name(), profileData.getLogin_id());
        mTextViewCompany.setText(profileData.getCompany());
        mTextViewPosition.setText(profileData.getRank_title());
        if (profileData.getPhone_mobile() != null && profileData.getPhone_mobile().isEmpty() == false)
            mTextViewCellphone.setText(profileData.getPhone_mobile());
        else
            mTextViewCellphone.setText(getString(R.string.setting_my_home_profile_empty_phone));
    }

    HFMyHomeHeaderView.OnMyHomeClickListener onMyHomeClickListener = new HFMyHomeHeaderView.OnMyHomeClickListener() {
        @Override
        public void onPlusClick() {
            HFActivityUtil.startAddTaskFromMyHomeActivity(MyHomeActivity.this, mProfileData, null);
        }
    };

    SwipeRefreshLayout.OnRefreshListener onOkrRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPresenter.initDataOkr(mMemberId, false);
        }
    };

    SwipeRefreshLayout.OnRefreshListener onTodoRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPresenter.initDataTodo(mMemberId, false);
        }
    };

    SwipeRefreshLayout.OnRefreshListener onRequestRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPresenter.initDataRequest(mMemberId, false);
        }
    };

    SwipeRefreshLayout.OnRefreshListener onProfileRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPresenter.initDataProfile(mMemberId, false);
        }
    };

    /**************************************************
     * MainHomeContract.OnMainHomeListListener
     **************************************************/
    MainHomeContract.OnMainHomeListListener onMainHomeListListener = new MainHomeContract.OnMainHomeListListener() {
        @Override
        public void onItemClicked(@NonNull TaskData data) {
            HFActivityUtil.startTaskDetailActivity(MyHomeActivity.this, data);
        }

        @Override
        public void updateTodoTaskToTodayTask(@NonNull TaskData data, boolean isChecked) {

        }

        @Override
        public void onTaskAcceptClicked(@NonNull TaskData data) {

        }

        @Override
        public void onTaskRejectClicked(@NonNull TaskData data) {

        }

        @Override
        public void onEvalAcceptClicked(@NonNull TaskData data) {

        }

        @Override
        public void onEvalRejectClicked(@NonNull TaskData data) {

        }

        @Override
        public void onEvalRevaluationClicked(@NonNull TaskData data) {

        }

        @Override
        public void onDeleteRequestClicked(@NonNull TaskData data) {

        }

        @Override
        public void onReRequestClicked(@NonNull TaskData data) {

        }
    };

    /**************************************************
     * PagerAdapter
     **************************************************/
    class MyHomePagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (position == INDEX_OKR) {
                container.addView(mViewOkr);
                return mViewOkr;
            } else if (position == INDEX_TODO) {
                container.addView(mViewTodo);
                return mViewTodo;
            } else if (position == INDEX_REQUEST) {
                container.addView(mViewRequest);
                return mViewRequest;
            } else {
                container.addView(mViewProfile);
                return mViewProfile;
            }
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            ((ViewPager) container).removeView((View) object);
        }
    }
}
