package com.cpm.lorealbaautomation.leavemanagement;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;

import android.view.MenuItem;
import android.view.View;

import com.cpm.lorealbaautomation.Database.Lorealba_Database;
import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.constant.CommonFunctions;
import com.cpm.lorealbaautomation.constant.CommonString;
import com.cpm.lorealbaautomation.databinding.ActivityLeaveCalenderBinding;
import com.cpm.lorealbaautomation.databinding.ActivityLeaveManagemntBinding;
import com.cpm.lorealbaautomation.delegates.PrefHelper;
import com.cpm.lorealbaautomation.gsonGetterSetter.leaves.TLeaveLedgerSummary;

import java.util.Objects;

public class LeaveManagemntActivity extends AppCompatActivity implements View.OnClickListener {
    String counter_Id, visit_date, username;
    Lorealba_Database db;
    private ActivityLeaveManagemntBinding binding = null;
    String from = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLeaveManagemntBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        db = new Lorealba_Database(this);
        db.open();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (!CommonFunctions.isTablet(this)) {
            toolbar.setTitleTextAppearance(this, R.style.changestext_sizefor_mobile);
        }
        if (getIntent().getStringExtra(CommonString.TAG_FROM) != null) {
            from = getIntent().getStringExtra(CommonString.TAG_FROM);
        }
        iduserinterface();
        setTitle(from.isEmpty() ? "Leave Management - " + username : "Leave Ledger - " + username);
    }

    private void iduserinterface() {
        SharedPreferences preferences = PrefHelper.getPrefs(this);
        counter_Id = preferences.getString(CommonString.KEY_COUNTER_ID, "");
        username = preferences.getString(CommonString.KEY_USERNAME, "");
        visit_date = preferences.getString(CommonString.KEY_DATE, "");

        if (!from.isEmpty()) {
            binding.contentLeave.cardApplyLeave.setVisibility(View.INVISIBLE);
        } else {
            binding.contentLeave.cardApplyLeave.setVisibility(View.VISIBLE);
        }
        binding.contentLeave.cardApplyLeave.setOnClickListener(this);
        binding.contentLeave.cardLeaveStatus.setOnClickListener(this);
        db.open();
    }

    @Override
    protected void onResume() {
        super.onResume();
        db.open();
        leaveLedgerDetails();
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.card_apply_leave) {
            startActivity(new Intent(this, ApplyForLeaveActivity.class));
            overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
        } else if (view.getId() == R.id.card_leave_status) {
            startActivity(new Intent(this, LeaveCalenderViewActivity.class));
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == android.R.id.home) {
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
            LeaveManagemntActivity.this.finish();
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
        LeaveManagemntActivity.this.finish();
    }

    @SuppressLint("SetTextI18n")
    private void leaveLedgerDetails() {
        db.open();
        TLeaveLedgerSummary personalLeave = db.getLeaveLedger(username, true);
        TLeaveLedgerSummary leaveLedgerTotal = db.getLeaveLedgerTotal(username);
        binding.contentLeave.tvHeader.setText(getString(R.string.leaveLedgerDetails));
        if (personalLeave != null) {
            binding.contentLeave.rlPersonalLeave.setVisibility(View.VISIBLE);
            binding.contentLeave.tvPersonalLeave.setText(personalLeave.getLeaveType());
            binding.contentLeave.earnedleaveTV.setText(personalLeave.getEarned().toString());
            binding.contentLeave.takenmonthleaveTV.setText(personalLeave.getTakenThisMonth().toString());
            binding.contentLeave.totleavetakenTV.setText(personalLeave.getTotalLeavesTaken().toString());
            binding.contentLeave.balanceleaveTV.setText(personalLeave.getBalance().toString());
        } else {
            binding.contentLeave.rlPersonalLeave.setVisibility(View.GONE);
        }

        TLeaveLedgerSummary emergencyLeave = db.getLeaveLedger(username, false);
        if (personalLeave != null) {
            binding.contentLeave.rlEmrLeave.setVisibility(View.VISIBLE);
            binding.contentLeave.tvEmrLeave.setText(emergencyLeave.getLeaveType());
            binding.contentLeave.earnedleaveSLTV.setText(emergencyLeave.getEarned().toString());
            binding.contentLeave.takenmonthleaveSLTV.setText(emergencyLeave.getTakenThisMonth().toString());
            binding.contentLeave.totleavetakenSLTV.setText(emergencyLeave.getTotalLeavesTaken().toString());
            binding.contentLeave.balanceleaveSLTV.setText(emergencyLeave.getBalance().toString());
        } else binding.contentLeave.rlEmrLeave.setVisibility(View.GONE);

        if (leaveLedgerTotal != null) {
            binding.contentLeave.rlTotalLeave.setVisibility(View.VISIBLE);
            binding.contentLeave.totleavetakentot.setText(leaveLedgerTotal.getTotalLeavesTaken().toString());
            binding.contentLeave.takenmonthleaveTotTV.setText(leaveLedgerTotal.getTakenThisMonth().toString());
            binding.contentLeave.earnedleaveTotalTV.setText(leaveLedgerTotal.getEarned().toString());
            binding.contentLeave.balanceleaveTotTV.setText(leaveLedgerTotal.getBalance().toString());
        } else {
            binding.contentLeave.rlTotalLeave.setVisibility(View.GONE);
        }

        if (emergencyLeave != null || personalLeave != null) {
            binding.contentLeave.tvNodata.setVisibility(View.GONE);
            binding.contentLeave.rlLeaveLeadger.setVisibility(View.VISIBLE);
        } else {
            binding.contentLeave.rlLeaveLeadger.setVisibility(View.GONE);
            binding.contentLeave.tvNodata.setVisibility(View.VISIBLE);
        }
    }
}
