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

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

import java.lang.reflect.Field;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.MainProjectListAdapter;
import kr.co.hanwha.hifive.adapter.ProjectSpinnerAdapter;
import kr.co.hanwha.hifive.adapter.contract.MainProjectListContract;
import kr.co.hanwha.hifive.base.BaseFragment;
import kr.co.hanwha.hifive.data.model.ProjectInfo;
import kr.co.hanwha.hifive.data.model.ProjectTaskInfo;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.dialog.HFProjectColorDialog;
import kr.co.hanwha.hifive.dialog.HFSingleDialog;
import kr.co.hanwha.hifive.enums.ProjectEvaluationType;
import kr.co.hanwha.hifive.enums.ProjectFilterType;
import kr.co.hanwha.hifive.network.HFNetworkUtil;
import kr.co.hanwha.hifive.screen.main.dashboard.DashboardProjectActivity;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;

/**
 * 메인 - Project View
 * <p>
 *
 * @see MainProjectPresenter
 * Created by sanghoonkwon on 2018. 2. 19..
 */

public class MainProjectFragment extends BaseFragment implements MainProjectContract.View {
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private MainProjectListAdapter mAdapterList;
    private TextView mTextViewEmpty;
    private View mViewBackground;

    private MainProjectPresenter mPresenter;
    private int mProjectId = 0;

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

        mAdapterList = new MainProjectListAdapter(getActivity(), onMainProjectListListener);

        mRefreshLayout = (SwipeRefreshLayout) parent.findViewById(R.id.swipeRefreshLayout);
        mRefreshLayout.setOnRefreshListener(onRefreshListener);

        mRecyclerView = (RecyclerView) parent.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mAdapterList);

        mTextViewEmpty = (TextView) parent.findViewById(R.id.empty_textView);

        mViewBackground = (View) parent.findViewById(R.id.background_view);

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

        mPresenter = new MainProjectPresenter(getActivity(), this, mAdapterList, mAdapterList);
        if (mProjectId != 0) mPresenter.setProjectId(mProjectId);
        else
            mPresenter.initProjectList();

        return parent;
    }

    @Override
    public void setProjectId(int projectId) {
        mProjectId = projectId;
    }

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

        if (HFInfoUtil.getNeedRefreshMainProjectList(getActivity()))
            mPresenter.initTaskData(true);
    }

    /**
     * View
     */
    @Override
    public void setRefreshing(boolean refreshing) {
        mRefreshLayout.setRefreshing(refreshing);
    }

    @Override
    public void setBackgroundView(boolean visible) {
        mViewBackground.setVisibility(visible?View.VISIBLE: View.GONE);
    }

    @Override
    public void setEmptyView(boolean empty) {
        if (empty) {
            mTextViewEmpty.setVisibility(View.VISIBLE);
            mRefreshLayout.setVisibility(View.GONE);
        } else {
            mTextViewEmpty.setVisibility(View.GONE);
            mRefreshLayout.setVisibility(View.VISIBLE);
        }
    }

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

    /**************************************************
     * MainProjectContract.OnMainProjectListListener
     **************************************************/
    MainProjectListContract.OnMainProjectListListener onMainProjectListListener = new MainProjectListContract.OnMainProjectListListener() {
        @Override
        public void onProjectItemSelected(@NonNull int project_id) {
            mPresenter.setProjectId(project_id);
        }

        @Override
        public void onItemClicked(@NonNull ProjectTaskInfo data) {
            HFActivityUtil.startTaskDetailActivity(getActivity(), data.getId(), 0, 0);
        }

        @Override
        public void onProjectColorClicked() {
            mPresenter.showProjectColorDialog();
        }

        @Override
        public void onEditIssueClicked() {
            HFInfoUtil.setNeedRefreshMainProjectList(getActivity());
            mPresenter.showEditIssueDialog();
        }

        @Override
        public void onFilterClicked(@NonNull ProjectFilterType filterType) {
            if (filterType == ProjectFilterType.IN_PROGRESS)
                mPresenter.setIsAll(false);
            else if (filterType == ProjectFilterType.ALL)
                mPresenter.setIsAll(true);
        }
    };

    /**************************************************
     * 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;
            }

        }
    };
}