package com.tofly.ghrq_route.module.emergencyHandle.ui.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.emergencyHandle.domain.MaintainExecuteBean;
import com.tofly.ghrq_route.module.emergencyHandle.ui.UiEmergencyRepairHandleList;
import com.tofly.ghrq_route.module.emergencyHandle.ui.adapter.EmergencyRepairHandleAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/2/21.
 */

public class EmergencyRepairHandleHistoryModuleLayout extends LinearLayout {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.layout_check_more)
    LinearLayout layoutCheckMore;
    @BindView(R.id.layout_check_report_module)
    LinearLayout layoutCheckReportModule;

    private Context mContext;

    private EmergencyRepairHandleAdapter repairHandleAdapter;

    private OnClickListener onRetryListener;

    private EmergencyHandleData mEmergencyData;

    public EmergencyRepairHandleHistoryModuleLayout(Context context) {
        this(context, null);
    }

    public EmergencyRepairHandleHistoryModuleLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmergencyRepairHandleHistoryModuleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        initView();
        initListener();
    }

    private void initView() {
        inflate(mContext, R.layout.layout_emergency_repair_handle_history_module, this);
        ButterKnife.bind(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    private void initListener() {

    }

    public void initEmergencyInfo(EmergencyHandleData handleData) {
        layoutCheckReportModule.setVisibility(GONE);
        if (handleData != null) {
            mEmergencyData = handleData;
            List<MaintainExecuteBean> executeHisList = handleData.getSchedulerMaintenanceHisList();
            if (executeHisList != null) {
                List<MaintainExecuteBean> logList = null;
                if (executeHisList.size() > 0) {
                    logList = executeHisList.subList(0, 1);
                }
                if (logList != null) {
                    repairHandleAdapter = new EmergencyRepairHandleAdapter(mContext, handleData, logList);
                    recyclerView.setAdapter(repairHandleAdapter);
                    layoutCheckReportModule.setVisibility(VISIBLE);
                }

                layoutCheckMore.setVisibility(executeHisList.size() > 1 ? VISIBLE : GONE);
            }
        }
    }

    private void gotoLogHistory() {
        if (mEmergencyData != null) {
            Bundle bundle = new Bundle();
            bundle.putSerializable(EmergencyHandleData.EMERGENCY_DATA_KEY, mEmergencyData);
            Intent intent = new Intent();
            intent.setClass(mContext, UiEmergencyRepairHandleList.class);
            intent.putExtras(bundle);
            mContext.startActivity(intent);
        }
    }

    @OnClick({R.id.layout_check_more})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_check_more:
                gotoLogHistory();
                break;
        }
    }
}
