package com.younki.maidmatching.ui.maid;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.younki.maidmatching.R;
import com.younki.maidmatching.databinding.ActivityMasterMaidBinding;
import com.younki.maidmatching.databinding.ActivityMasterOwnerBinding;
import com.younki.maidmatching.services.SharedPreference.Preferences;
import com.younki.maidmatching.ui.AddMaidSkillDialog;
import com.younki.maidmatching.ui.CompleteProfileDialog;
import com.younki.maidmatching.ui.MainActivity;
import com.younki.maidmatching.ui.owner.OwnerHomeFragment;
import com.younki.maidmatching.ui.owner.OwnerJobFragment;
import com.younki.maidmatching.ui.owner.OwnerMaidFragment;
import com.younki.maidmatching.ui.owner.OwnerProfileFragment;
import com.younki.maidmatching.utils.BaseActivity;

public class MasterMaidActivity extends BaseActivity {

    ActivityMasterMaidBinding binding;

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

        // Check Internet Status
        checkInternetStatus(MasterMaidActivity.this);
        checkPreferencesApp(MasterMaidActivity.this);

        if (Preferences.getUserSkills(MasterMaidActivity.this) <= 0) {
            AddMaidSkillDialog addMaidSkillDialog = new AddMaidSkillDialog(MasterMaidActivity.this);
            addMaidSkillDialog.setCancelable(false);
            addMaidSkillDialog.getWindow().setBackgroundDrawable(new ColorDrawable(MasterMaidActivity.this.getResources().getColor(android.R.color.transparent)));
            addMaidSkillDialog.show();
        }

        if (Preferences.getUserStatus(MasterMaidActivity.this) == 1) {
            CompleteProfileDialog completeProfileDialog = new CompleteProfileDialog(MasterMaidActivity.this);
            completeProfileDialog.setCancelable(false);
            completeProfileDialog.getWindow().setBackgroundDrawable(new ColorDrawable(MasterMaidActivity.this.getResources().getColor(android.R.color.transparent)));
            completeProfileDialog.show();
        }

        replaceFragment(new MaidHomeFragment());
        binding.bottomNavigationView.setBackground(null);
        binding.bottomNavigationView.setOnItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.home:
                    replaceFragment(new MaidHomeFragment());
                    break;
                case R.id.job:
                    replaceFragment(new MaidJobFragment());
                    break;
                case R.id.profile:
                    replaceFragment(new MaidProfileFragment());
                    break;
            }
            return true;
        });

        AlertDialog.Builder mBuilder = new AlertDialog.Builder(MasterMaidActivity.this);
        View mView = getLayoutInflater().inflate(R.layout.dialog_dont_show_again, null);
        CheckBox mCheckBox = mView.findViewById(R.id.checkBox);
        mBuilder.setTitle("Lakukan verifikasi data / KYC!");
        mBuilder.setMessage("Segera lakukan verifikasi data / KYC di menu profile, untuk membuktikan keseriusan anda!\nData anda sepenuhnya akan dijaga kerahasiaannya.");
        mBuilder.setView(mView);
        mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        AlertDialog mDialog = mBuilder.create();
        mDialog.show();

        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    storeDialogStatus(true);
                }else{
                    storeDialogStatus(false);
                }
            }
        });

        if(getDialogStatus()){
            mDialog.hide();
        }else{
            mDialog.show();
        }
    }

    private void storeDialogStatus(boolean isChecked){
        SharedPreferences mSharedPreferences = getSharedPreferences("CheckItem", MODE_PRIVATE);
        SharedPreferences.Editor mEditor = mSharedPreferences.edit();
        mEditor.putBoolean("item", isChecked);
        mEditor.apply();
    }

    private boolean getDialogStatus(){
        SharedPreferences mSharedPreferences = getSharedPreferences("CheckItem", MODE_PRIVATE);
        return mSharedPreferences.getBoolean("item", false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkPreferencesApp(MasterMaidActivity.this);
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frame_layout, fragment);
        fragmentTransaction.commit();
    }
}