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.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.MainProjectListAdapter;
import kr.co.hanwha.hifive.adapter.contract.MainProjectListContract;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.ProjectTaskInfo;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.enums.ProjectFilterType;
import kr.co.hanwha.hifive.screen.main.dashboard.contract.DashboardProjectContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;

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

public class DashboardProjectActivity extends BaseActivity implements DashboardProjectContract.View {
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private MainProjectListAdapter mAdapterList;
    private TextView mTextViewEmpty;

    private DashboardProjectPresenter mPresenter;
    private String mProjectName = "";
    private int mProjectEvaluation = 0;
    private int mProjectId = 0;

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

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

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

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

        mAdapterList = new MainProjectListAdapter(this, onMainProjectListListener);

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

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

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

        mProjectId = getIntent().getIntExtra(HFActivityUtil.INTENT_PROJECT_ID, 0);
        mPresenter = new DashboardProjectPresenter(this, mAdapterList, mAdapterList);
        mPresenter.initTaskData(mProjectId, true);
    }

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

        if (HFInfoUtil.getNeedRefreshMainProjectList(this)) {
            HFInfoUtil.setNeedRefreshMainDashboardList(this);
            mPresenter.initTaskData(mProjectId, true);
        }
    }

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

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

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

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

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

        @Override
        public void onEditIssueClicked() {
            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);
        }
    };
}
