package kr.co.hanwha.hifive.screen.main.dashboard;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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 kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.MainDashboardListAdapter;
import kr.co.hanwha.hifive.adapter.contract.MainDashboardListContract;
import kr.co.hanwha.hifive.base.BaseFragment;
import kr.co.hanwha.hifive.base.FirebaseAnalyticsInterface;
import kr.co.hanwha.hifive.data.model.DabProjectData;
import kr.co.hanwha.hifive.screen.main.dashboard.contract.MainDashboardContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFAnalyticsUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;
import kr.co.hanwha.hifive.view.HFDashboardTabView;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

/**
 * Created by sanghoonkwon on 2018. 3. 15..
 */

public class MainDashboardFragment extends BaseFragment implements MainDashboardContract.View, FirebaseAnalyticsInterface {
    private String mClassUri = "";
    private ViewPager mViewPager;
    private View mViewGreen;
    private View mViewOrange;
    private SwipeRefreshLayout mRefreshLayoutGreen;
    private SwipeRefreshLayout mRefreshLayoutOrange;
    private RecyclerView mRecyclerViewGreen;
    private RecyclerView mRecyclerViewOrange;
    private MainDashboardListAdapter mAdapterGreen;
    private MainDashboardListAdapter mAdapterOrange;
    private LinearLayoutManager mLayoutManagerGreen;
    private LinearLayoutManager mLayoutManagerOrange;
    private LinearLayout mLayoutEmptyGreen;
    private LinearLayout mLayoutEmptyOrange;
    private LinearLayout mLayoutSort;
    private TextView mTextViewSort;

    private HFDashboardTabView mDashboardTabView;

    private MainDashboardPresenter mPresenter;

    private final int INDEX_ORANGE = 0;
    private final int INDEX_GREEN = 1;

    private boolean isDescendingOrder = true;

    @Override
    public String getClassUri() {
        return mViewPager.getCurrentItem() == INDEX_ORANGE ? HFAnalyticsUtil.HF_GNB_DASHBOARD_REDYELLOW : HFAnalyticsUtil.HF_GNB_DASHBOARD_GREEN;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View parent = inflater.inflate(R.layout.fragment_main_dashboard, container, false);

        mDashboardTabView = (HFDashboardTabView) parent.findViewById(R.id.dashboardTabView);
        mDashboardTabView.setOnMainTabListener(onMainTabListener);

        mLayoutSort = (LinearLayout) parent.findViewById(R.id.sort_linearLayout);
        mLayoutSort.setOnClickListener(onClickListener);

        mTextViewSort = (TextView) parent.findViewById(R.id.sort_textView);

        LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
        mViewGreen = layoutInflater.inflate(R.layout.fragment_main_dashboard_sub, container, false);
//        mViewOrange = layoutInflater.inflate(R.layout.fragment_main_dashboard_sub, container, false);

        //Green
        mAdapterGreen = new MainDashboardListAdapter(getActivity(), onMainDashboardListListener);
        mRefreshLayoutGreen = (SwipeRefreshLayout) mViewGreen.findViewById(R.id.swipeRefreshLayout);
        mRefreshLayoutGreen.setOnRefreshListener(onRefreshListener);

        mLayoutManagerGreen = new LinearLayoutManager(getActivity());
        mRecyclerViewGreen = (RecyclerView) mViewGreen.findViewById(R.id.recyclerView);
        mRecyclerViewGreen.setLayoutManager(mLayoutManagerGreen);
        mRecyclerViewGreen.setAdapter(mAdapterGreen);

        mLayoutEmptyGreen = (LinearLayout) mViewGreen.findViewById(R.id.green_empty_layout);

        // Orange
//        mAdapterOrange = new MainDashboardListAdapter(getActivity(), onMainDashboardListListener);
//        mRefreshLayoutOrange = (SwipeRefreshLayout) mViewOrange.findViewById(R.id.swipeRefreshLayout);
//        mRefreshLayoutOrange.setOnRefreshListener(onRefreshListener);
//
//        mLayoutManagerOrange = new LinearLayoutManager(getActivity());
//        mRecyclerViewOrange = (RecyclerView) mViewOrange.findViewById(R.id.recyclerView);
//        mRecyclerViewOrange.setLayoutManager(mLayoutManagerOrange);
//        mRecyclerViewOrange.setAdapter(mAdapterOrange);
//
//        mLayoutEmptyOrange = (LinearLayout) mViewOrange.findViewById(R.id.orange_empty_layout);

        mViewPager = (ViewPager) parent.findViewById(R.id.viewPager);
        MainPagerAdapter pagerAdapter = new MainPagerAdapter();
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.addOnPageChangeListener(onPageChangeListener);
        mViewPager.setOffscreenPageLimit(1);

        parent.findViewById(R.id.alarm_imageButton).setOnClickListener(onClickListener);
        parent.findViewById(R.id.search_imageButton).setOnClickListener(onClickListener);

        mPresenter = new MainDashboardPresenter(getActivity(), this,
                mAdapterGreen, mAdapterGreen,
                mAdapterOrange, mAdapterOrange);
        mPresenter.initData(true, isDescendingOrder);

        return parent;
    }

    @Override
    public void onResume() {
        super.onResume();

        if (HFInfoUtil.getNeedRefreshMainDashboardList(getActivity())) {
            mPresenter.initData(true, isDescendingOrder);
        }

        HFAnalyticsUtil.logEvent(this);
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        mRefreshLayoutGreen.setRefreshing(refreshing);
//        mRefreshLayoutOrange.setRefreshing(refreshing);
    }

    @Override
    public void setEmptyViewOrange(boolean empty) {
        if (empty) {
            mLayoutEmptyOrange.setVisibility(View.VISIBLE);
            mRefreshLayoutOrange.setVisibility(View.GONE);
        } else {
            mLayoutEmptyOrange.setVisibility(View.GONE);
            mRefreshLayoutOrange.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void setEmptyViewGreen(boolean empty) {
        if (empty) {
            mLayoutEmptyGreen.setVisibility(View.VISIBLE);
            mRefreshLayoutGreen.setVisibility(View.GONE);
        } else {
            mLayoutEmptyGreen.setVisibility(View.GONE);
            mRefreshLayoutGreen.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initSortText() {
        isDescendingOrder = true;
        setSortText();
    }

    @Override
    public void setSortText() {
        if (isDescendingOrder) {
            mTextViewSort.setText(R.string.main_dashboard_descending_order);
            mTextViewSort.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_arr_high, 0);
        } else {
            mTextViewSort.setText(R.string.main_dashboard_ascending_order);
            mTextViewSort.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_arr_low, 0);
        }
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.alarm_imageButton:
                    HFActivityUtil.startAlarmActivity(getActivity());
                    break;
                case R.id.search_imageButton:
                    HFActivityUtil.startSearchActivity(getActivity());
                    break;
                case R.id.sort_linearLayout:
                    isDescendingOrder = !isDescendingOrder;
                    mPresenter.reverseSort();
                    setSortText();
                    break;
            }

        }
    };

    /**************************************************
     * OnRefreshListener
     **************************************************/
    SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPresenter.initData(false, isDescendingOrder);
        }
    };

    /**************************************************
     * HFMainTabFontView.OnMainTabListener
     **************************************************/
    HFDashboardTabView.OnMainTabListener onMainTabListener = new HFDashboardTabView.OnMainTabListener() {
        @Override
        public void onTabSelected(int position) {
            mViewPager.setCurrentItem(position);
        }
    };

    /**************************************************
     * MainDashboardListContract.OnMainDashboardListListener
     **************************************************/
    MainDashboardListContract.OnMainDashboardListListener onMainDashboardListListener = new MainDashboardListContract.OnMainDashboardListListener() {
        @Override
        public void onItemClicked(@NonNull DabProjectData data) {
            if (data == null) return;
            HFAnalyticsUtil.logEvent(MainDashboardFragment.this, getClassUri() + "/" + data.getProject_id());
            HFActivityUtil.startDashboardProjectActivity(getActivity(), data.getProject_id());
        }
    };

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

        @Override
        public void onPageSelected(int position) {
            mDashboardTabView.setCurrentItem(position);
            HFAnalyticsUtil.logEvent(MainDashboardFragment.this);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewGreen);
            return mViewGreen;
//            if (position == INDEX_GREEN) {
//                container.addView(mViewGreen);
//                return mViewGreen;
//            } else {
//                container.addView(mViewOrange);
//                return mViewOrange;
//            }
        }
    }
}