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.RushRepairHistoryAdapter;
import com.tofly.ghrq_route.module.OfflineWork.domain.OfflineWorkJobBean;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairBean;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairProcessBean;
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 UiRushRepairHistory extends BaseUi {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;

    private RushRepairHistoryAdapter adapter;
    private List<RushRepairBean> data;
    private DBDaoHelper dbDaoHelper;
    private List<OfflineWorkJobBean> offlineWorkJobBeanList1, offlineWorkJobBeanList2;

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

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

    private void initData() {
        data.clear();
        offlineWorkJobBeanList1 = dbDaoHelper.getOfflineWorkJobs(OfflineWorkJobBean.OfflineWorkJobType.JOB_RUSH_REPAIR_ADD);
        offlineWorkJobBeanList2 = dbDaoHelper.getOfflineWorkJobs(OfflineWorkJobBean.OfflineWorkJobType.JOB_RUSH_REPAIR_PROCESS);
        if (offlineWorkJobBeanList1 != null && offlineWorkJobBeanList1.size() > 0) {
            for (OfflineWorkJobBean offlineWorkJobBean : offlineWorkJobBeanList1) {
                RushRepairBean rushRepairBean = dbDaoHelper.getRushRepairBean(offlineWorkJobBean.getRef_id());
                data.add(rushRepairBean);
            }
        }
        if (offlineWorkJobBeanList2 != null && offlineWorkJobBeanList2.size() > 0) {
            for (OfflineWorkJobBean offlineWorkJobBean : offlineWorkJobBeanList2) {
                RushRepairProcessBean rushRepairProcessBean = dbDaoHelper.getRushRepairProcess(offlineWorkJobBean.getRef_id());
                RushRepairBean rushRepairBean = dbDaoHelper.getRushRepairBean(rushRepairProcessBean.getWorkOrderId());
                data.add(rushRepairBean);
            }
        }
        adapter.setData(data);
    }

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