package com.tofly.ghrq_route.common.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lcp.arecyclerview.widget.ARecycleView;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.common.ui.adapter.BuildHistoryAdapter;
import com.tofly.ghrq_route.module.OfflineWork.domain.OfflineWorkJobBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildProcessManageBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author YCHK
 * 工地监护历史记录
 */
public class UiBuildHistoryActivity extends BaseUi {

    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;

    private BuildHistoryAdapter adapter;
    private List<BuildManageBean> data;
    private DBDaoHelper dbDaoHelper;
    private List<OfflineWorkJobBean> offlineWorkJobBeanList, offlineWorkJobBeanList1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_history);
        ButterKnife.bind(this);
        initListener();
    }

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

    private void initData() {
        data = new ArrayList<>();
        offlineWorkJobBeanList = dbDaoHelper.getOfflineWorkJobs(OfflineWorkJobBean.OfflineWorkJobType.JOB_BUILD_MANAGE_ADD);
        offlineWorkJobBeanList1 = dbDaoHelper.getOfflineWorkJobs(OfflineWorkJobBean.OfflineWorkJobType.JOB_BUILD_MANAGE_PROCESS);
        if (offlineWorkJobBeanList != null && offlineWorkJobBeanList.size() > 0) {
            for (OfflineWorkJobBean offlineWorkJobBean : offlineWorkJobBeanList) {
                BuildManageBean buildManageBean = dbDaoHelper.getBuildManage(offlineWorkJobBean.getRef_id());
                data.add(buildManageBean);
            }
        }
        if (offlineWorkJobBeanList1 != null && offlineWorkJobBeanList1.size() > 0) {
            for (OfflineWorkJobBean offlineWorkJobBean : offlineWorkJobBeanList1) {
                BuildProcessManageBean buildProcessManageBean = dbDaoHelper.getBuildProcess(offlineWorkJobBean.getRef_id());
                BuildManageBean buildManageBean = dbDaoHelper.getBuildManage(buildProcessManageBean.getWorkOrderId());
                data.add(buildManageBean);
            }
        }
        adapter.setData(data);
    }

    private void initListener() {
        dbDaoHelper = DBDaoHelper.getInstance();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        data = new ArrayList<>();
        adapter = new BuildHistoryAdapter(this, data);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((view, position) -> {
            BuildManageBean buildManageBean = adapter.getItem(position);
            if (buildManageBean != null) {
                Intent intent = new Intent(this, UiBuildHistoryDetailActivity.class);
                intent.putExtra("build_manage", buildManageBean);
                int size = 0;
                if (offlineWorkJobBeanList != null) {
                    size = offlineWorkJobBeanList.size();
                }
                if (TextUtils.isEmpty(buildManageBean.getConstructionPerson())) {
                    if (offlineWorkJobBeanList.get(position).getRef_photo_ids() != null) {
                        intent.putStringArrayListExtra("photoId", (ArrayList<String>) offlineWorkJobBeanList.get(position).getRef_photo_ids());
                    }
                    if (offlineWorkJobBeanList.get(position).getRef_audio_ids() != null) {
                        intent.putStringArrayListExtra("audioId", (ArrayList<String>) offlineWorkJobBeanList.get(position).getRef_audio_ids());
                    }
                    if (offlineWorkJobBeanList.get(position).getRef_data_ids() != null) {
                        intent.putStringArrayListExtra("fileId", (ArrayList<String>) offlineWorkJobBeanList.get(position).getRef_data_ids());
                    }
                } else {
                    if (offlineWorkJobBeanList1.get(position - size).getRef_photo_ids() != null) {
                        intent.putStringArrayListExtra("photoId", (ArrayList<String>) offlineWorkJobBeanList1.get(position - size).getRef_photo_ids());
                    }
                    if (offlineWorkJobBeanList1.get(position - size).getRef_audio_ids() != null) {
                        intent.putStringArrayListExtra("audioId", (ArrayList<String>) offlineWorkJobBeanList1.get(position - size).getRef_audio_ids());
                    }
                    if (offlineWorkJobBeanList1.get(position - size).getRef_data_ids() != null) {
                        intent.putStringArrayListExtra("fileId", (ArrayList<String>) offlineWorkJobBeanList1.get(position - size).getRef_data_ids());
                    }
                    if (!TextUtils.isEmpty(offlineWorkJobBeanList1.get(position - size).getRef_id())) {
                        intent.putExtra("id", offlineWorkJobBeanList1.get(position - size).getRef_id());
                    }
                }
                startActivity(intent);
            }
        });
    }

}
