package ru.myitschool.work.ui.employee;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentResultListener;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import com.squareup.picasso.Picasso;

import ru.myitschool.work.R;
import ru.myitschool.work.data.dto.EmployeeDataDto;
import ru.myitschool.work.databinding.FragmentEmployeeBinding;
import ru.myitschool.work.ui.qr.scan.QrScanDestination;

public class EmployeeFragment extends Fragment {

    private static final String SHARED_PREFERENCES_LOGIN_PREFERENCES = "l01";
    private static final String SHARED_PREFERENCES_LOGIN_STRING = "l02";
    private static final String SHARED_PREFERENCES_CODE = "l03";


    private FragmentEmployeeBinding binding;
    private EmployeeViewModel viewModel;

    public EmployeeFragment() {
        super(R.layout.fragment_employee);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding = FragmentEmployeeBinding.bind(view);
        viewModel = new ViewModelProvider(this).get(EmployeeViewModel.class);


        binding.logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getContext()
                        .getSharedPreferences(SHARED_PREFERENCES_LOGIN_PREFERENCES, Context.MODE_PRIVATE)
                        .edit()
                        .remove(SHARED_PREFERENCES_LOGIN_STRING).apply();

                Navigation.findNavController(getView()).navigate(
                        R.id.action_employee_fragment_to_login_fragment);
            }
        });

        binding.refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.startQuery(getContext()
                        .getSharedPreferences(SHARED_PREFERENCES_LOGIN_PREFERENCES, Context.MODE_PRIVATE)
                        .getString(SHARED_PREFERENCES_LOGIN_STRING, ""));
            }
        });


        binding.scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(getView()).navigate(
                        R.id.action_employee_fragment_to_qr_fragment);
            }
        });

        getParentFragmentManager().setFragmentResultListener(QrScanDestination.REQUEST_KEY, this, new FragmentResultListener() {
            @Override
            public void onFragmentResult(@NonNull String key, @NonNull Bundle bundle) {
                SharedPreferences sharedPreferences = getContext().getSharedPreferences(SHARED_PREFERENCES_LOGIN_PREFERENCES, Context.MODE_PRIVATE);
                sharedPreferences.edit()
                        .putString(SHARED_PREFERENCES_CODE, QrScanDestination.INSTANCE.getDataIfExist(bundle))
                        .apply();
                Navigation.findNavController(getView()).navigate(R.id.action_employee_fragment_to_result_fragment);
            }
        });


        subscribe(viewModel);
        viewModel.startQuery(getContext()
                .getSharedPreferences(SHARED_PREFERENCES_LOGIN_PREFERENCES, Context.MODE_PRIVATE)
                .getString(SHARED_PREFERENCES_LOGIN_STRING, ""));
    }

    private void subscribe(EmployeeViewModel viewModel) {
        viewModel.isSuccessfulReceiptLiveData.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean value) {
                if (value) {
                    binding.fullname.setVisibility(View.VISIBLE);
                    binding.photo.setVisibility(View.VISIBLE);
                    binding.position.setVisibility(View.VISIBLE);
                    binding.lastEntry.setVisibility(View.VISIBLE);
                    binding.logout.setVisibility(View.VISIBLE);
                    binding.scan.setVisibility(View.VISIBLE);

                    binding.error.setVisibility(View.GONE);
                } else {
                    binding.error.setVisibility(View.VISIBLE);
                    binding.error.setText("error");

                    binding.fullname.setVisibility(View.GONE);
                    binding.photo.setVisibility(View.GONE);
                    binding.position.setVisibility(View.GONE);
                    binding.lastEntry.setVisibility(View.GONE);
                    binding.logout.setVisibility(View.GONE);
                    binding.scan.setVisibility(View.GONE);
                }
            }
        });

        viewModel.employeeLiveData.observe(getViewLifecycleOwner(), new Observer<EmployeeDataDto>() {
            @Override
            public void onChanged(EmployeeDataDto value) {
                binding.fullname.setText(value.getName());
                Picasso.get().load(value.getPhoto()).into(binding.photo);
                binding.position.setText(value.getEmployeePosition());
                binding.lastEntry.setText(value.getLastVisit().toString());
            }
        });

        viewModel.amIAdminLiveData.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean value) {
                binding.adminPanel.setVisibility(value ? View.VISIBLE : View.GONE);
            }
        });
    }


    @Override
    public void onDestroyView() {
        binding = null;
        super.onDestroyView();
    }
}