package com.fusi24.rfid.ui.scanresult;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

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

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.entity.DataProblems;
import com.fusi24.rfid.data.entity.DataResultRfid;
import com.fusi24.rfid.databinding.ActivityScanResultBinding;

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

public class ScanResultActivity extends BaseActivity implements ScanResultView {

    public static final String ID_SITE = "id_site";
    public static final String ID_PERMIT = "id_permit";
    public static final String RFID_CARD_NUMBER = "rfid_card_number";

    private ScanResultPresenter presenter;
    private ScanResultAdapter adapter;
    private List<DataProblems> problemsList;
    private String dataUrl;
    private String idSite;
    private String idPermit;
    private String numberRfid;
    private String numberRfidTemp;
    private ActivityScanResultBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityScanResultBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

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

        if (getIntent() != null){
            idSite = getIntent().getStringExtra(ID_SITE);
            idPermit = getIntent().getStringExtra(ID_PERMIT);
            numberRfidTemp = getIntent().getStringExtra(RFID_CARD_NUMBER);
        }

        // Setting digit rfid card
        if (numberRfidTemp != null){
            switch (numberRfidTemp.length()){
                case 5:
                    numberRfid = "00000" + numberRfidTemp;
                    break;
                case 6:
                    numberRfid = "0000" + numberRfidTemp;
                    break;
                case 7:
                    numberRfid = "000" + numberRfidTemp;
                    break;
                case 8:
                    numberRfid = "00" + numberRfidTemp;
                    break;
                case 9:
                    numberRfid = "0" + numberRfidTemp;
                    break;
                default:
                    numberRfid = numberRfidTemp;
                    break;
            }
        }

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

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

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

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

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

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

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

    @Override
    public void showDataRfid(DataResultRfid dataResultRfid) {

        if (dataResultRfid.getEmployee() != null){
            dataUrl = Constant.URL_PHOTO + dataResultRfid.getEmployee().getUrlPhoto().substring(14);

            binding.tvProfileName.setText(dataResultRfid.getEmployee().getName());
            binding.tvStructuralPosition.setText(dataResultRfid.getEmployee().getStructuralPosition().getName());
            binding.tvFunctionalPosition.setText(dataResultRfid.getEmployee().getFunctionalPosition().getName());
            binding.tvCompanyName.setText(dataResultRfid.getEmployee().getCompany().getName());
        } else {
            binding.tvProfileName.setText("-");
            binding.tvStructuralPosition.setText("-");
            binding.tvFunctionalPosition.setText("-");
            binding.tvCompanyName.setText("-");
        }

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

        if (dataResultRfid.getPassed()){
            binding.tvProfileStatus.setText(R.string.label_status_passed);
            binding.llProfileStatus.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        } else {
            if (dataResultRfid.getAlertType() != null){
                if (dataResultRfid.getAlertType().equalsIgnoreCase("ERROR")){
                    binding.tvProfileStatus.setText(R.string.label_status_not_passed);
                    binding.llProfileStatus.setBackgroundColor(getResources().getColor(R.color.colorRedDark));
                }

                if (dataResultRfid.getAlertType().equalsIgnoreCase("WARNING")){
                    binding.tvProfileStatus.setText(R.string.label_status_not_valid);
                    binding.llProfileStatus.setBackgroundColor(getResources().getColor(R.color.colorRedDark));
                }

            } else {
                binding.tvProfileStatus.setText(R.string.label_status_not_passed);
                binding.llProfileStatus.setBackgroundColor(getResources().getColor(R.color.colorRedDark));
            }
        }

        if (dataResultRfid.getMessage() != null){
            Toast.makeText(this, dataResultRfid.getMessage(), Toast.LENGTH_SHORT).show();
        }

        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();
        binding = null;
    }
}
