package cpm.com.gskmtorange.pharma_stores;

import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import cpm.com.gskmtorange.R;
import cpm.com.gskmtorange.constant.CommonString;

public class PharmaActivity extends AppCompatActivity implements StoreDetailFragment.OnFragmentInteractionListener,
        MSLFragment.OnFragmentInteractionListener, SOSFragment.OnFragmentInteractionListener{
    private SharedPreferences.Editor editor = null;
    private SharedPreferences preferences;
    FloatingActionButton fab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paharma);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        preferences = PreferenceManager.getDefaultSharedPreferences(this);

        editor = preferences.edit();


        Bundle bundle = new Bundle();
        bundle.putString("categoryName", "Oral Health");
        bundle.putString("categoryId", "2");
        StoreDetailFragment fragment = new StoreDetailFragment();
        fragment.setArguments(bundle);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment).commit();

        fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment);
                if(f instanceof StoreDetailFragment){
                    Bundle bundle = new Bundle();
                    bundle.putString("categoryName", "Oral Health");
                    bundle.putString("categoryId", "2");
                    SOSFragment fragment = new SOSFragment();
                    fragment.setArguments(bundle);

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment).commit();
                }
                else if(f instanceof SOSFragment){
                    Bundle bundle = new Bundle();
                    bundle.putString("categoryName", "Oral Health");
                    bundle.putString("categoryId", "2");
                    MSLFragment fragment = new MSLFragment();
                    fragment.setArguments(bundle);

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment).commit();
                } else{
                    // do something with f
                    Snackbar.make(fab, "---",Snackbar.LENGTH_SHORT).show();
                }

            }
        });
    }

    @Override
    public void onFragmentInteraction(Fragment f) {

        if(f instanceof StoreDetailFragment){
            Bundle bundle = new Bundle();
            bundle.putString("categoryName", "Oral Health");
            bundle.putString("categoryId", "2");
            SOSFragment fragment = new SOSFragment();
            fragment.setArguments(bundle);

            getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment).commit();
        }
        else  if(f instanceof SOSFragment){
            Bundle bundle = new Bundle();
            bundle.putString("categoryName", "Oral Health");
            bundle.putString("categoryId", "2");
            MSLFragment fragment = new MSLFragment();
            fragment.setArguments(bundle);

            getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment).commit();
        }else if(f instanceof MSLFragment){
                    /*Bundle bundle = new Bundle();
                    bundle.putString("categoryName", "Oral Health");
                    bundle.putString("categoryId", "2");
                    MSLFragment fragment = new MSLFragment();
                    fragment.setArguments(bundle);

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment).commit();*/
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
    }
}
