package com.younki.maidmatching.ui.owner;

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.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.younki.maidmatching.R;
import com.younki.maidmatching.databinding.ActivityMasterOwnerBinding;
import com.younki.maidmatching.services.SharedPreference.Preferences;
import com.younki.maidmatching.ui.AddJobDetailDialog;
import com.younki.maidmatching.ui.CompleteProfileDialog;
import com.younki.maidmatching.ui.MainActivity;
import com.younki.maidmatching.ui.auth.LoginActivity;
import com.younki.maidmatching.ui.maid.MasterMaidActivity;
import com.younki.maidmatching.utils.BaseActivity;

public class MasterOwnerActivity extends BaseActivity {

    ActivityMasterOwnerBinding binding;

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

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

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

        replaceFragment(new OwnerHomeFragment());
        binding.bottomNavigationView.setBackground(null);
        binding.bottomNavigationView.setOnItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.home:
                    replaceFragment(new OwnerHomeFragment());
                    break;
                case R.id.maid:
                    replaceFragment(new OwnerMaidFragment());
                    break;
                case R.id.job:
                    replaceFragment(new OwnerJobFragment());
                    break;
                case R.id.profile:
                    replaceFragment(new OwnerProfileFragment());
                    break;
            }
            return true;
        });

        binding.fabOwner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showBottomDialog();
            }
        });

        AlertDialog.Builder mBuilder = new AlertDialog.Builder(MasterOwnerActivity.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(MasterOwnerActivity.this);
    }

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

    private void showBottomDialog() {

        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.btmdialogowner_layout);

        LinearLayout jobsLayout = dialog.findViewById(R.id.layoutAddJob);
        ImageView cancelButton = dialog.findViewById(R.id.cancelButton);

        jobsLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
                binding.bottomNavigationView.setSelectedItemId(R.id.fab);
                replaceFragment(new OwnerCreateJobFragment());

            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.show();
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().getAttributes().windowAnimations = R.style.Theme_MaidMatching;
        dialog.getWindow().setGravity(Gravity.BOTTOM);
    }
}