package com.fusi24.rfid.ui.scanresult;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

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

import com.bumptech.glide.Glide;
import com.fusi24.rfid.MainActivity;
import com.fusi24.rfid.R;
import com.fusi24.rfid.adapter.ScanResultAdapter;
import com.fusi24.rfid.base.BaseActivity;
import com.fusi24.rfid.config.Constant;
import com.fusi24.rfid.data.model.DataProblems;
import com.fusi24.rfid.data.model.DataResultRfid;
import com.google.gson.Gson;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class ScanResultActivity extends BaseActivity implements ScanResultView {

    @BindView(R.id.tv_rfid_number)
    TextView tvRfidNumber;

    @BindView(R.id.iv_profile_image)
    CircleImageView ivProfileImage;
    @BindView(R.id.tv_profile_name)
    TextView tvProfileName;
    @BindView(R.id.tv_structural_position)
    TextView tvStructuralPosition;
    @BindView(R.id.tv_functional_position)
    TextView tvFunctionalPosition;
    @BindView(R.id.tv_company_name)
    TextView tvCompanyName;

    @BindView(R.id.ll_profile_status)
    LinearLayout llProfileStatus;
    @BindView(R.id.tv_profile_status)
    TextView tvProfileStatus;

    @BindView(R.id.rv_document_problem)
    RecyclerView rvDocumentProblem;

    @BindView(R.id.btn_home)
    Button btnHome;
    @BindView(R.id.btn_scan_again)
    Button btnScanAgain;

    private ScanResultPresenter presenter;
    private ScanResultAdapter adapter;
    private List<DataProblems> problemsList;
    private String numberRfid;
    private Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_result);
        ButterKnife.bind(this);

        presenter = new ScanResultPresenter(
                getManager(), getAndroidScheduler(), getProcessScheduler());
        presenter.attachView(this);

        if (getIntent() != null){
            numberRfid = "000" + getIntent().getStringExtra("reception");
        }

        initView();
        loadingdata();
        initEvent();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){
        tvRfidNumber.setText("Nomor RFID : " + numberRfid);

        problemsList = new ArrayList<>();
        adapter = new ScanResultAdapter(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvDocumentProblem.setLayoutManager(layoutManager);
        rvDocumentProblem.setItemAnimator(new DefaultItemAnimator());
        rvDocumentProblem.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        rvDocumentProblem.setAdapter(adapter);
    }

    private void loadingdata(){
        problemsList.clear();
        String[] expand = {"employee.company, employee.structuralPosition, employee.functionalPosition"};
        presenter.checkRfidCard(numberRfid, 1, expand);
    }

    private void initEvent(){
        btnHome.setOnClickListener(v -> {
            final Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        });

        btnScanAgain.setOnClickListener(v -> {
            finish();
        });
    }

    @Override
    public void showDataRfid(DataResultRfid dataResultRfid) {

        String dataUrl = Constant.URL_PHOTO + dataResultRfid.getEmployee().getUrlPhoto().substring(14);

        Glide.with(this)
                .load(dataUrl)
                .placeholder(R.drawable.ic_profile_default)
                .error(R.drawable.ic_profile_default)
                .into(ivProfileImage);

        tvProfileName.setText(dataResultRfid.getEmployee().getName());
        tvStructuralPosition.setText(dataResultRfid.getEmployee().getStructuralPosition().getName());
        tvFunctionalPosition.setText(dataResultRfid.getEmployee().getFunctionalPosition().getName());
        tvCompanyName.setText(dataResultRfid.getEmployee().getCompany().getName());

        if (dataResultRfid.getPassed()){
            tvProfileStatus.setText(R.string.label_status_passed);
            llProfileStatus.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        } else {
            tvProfileStatus.setText(R.string.label_status_not_passed);
            llProfileStatus.setBackgroundColor(getResources().getColor(R.color.colorRedDark));
        }

        problemsList = dataResultRfid.getProblems();
        adapter.updateProblemList(problemsList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showError(Throwable e) {
        Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
