package com.pixelnx.localhelp.ui.vendor;

import androidx.appcompat.app.AlertDialog;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;

import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.StringRequestListener;
import com.bumptech.glide.Glide;
import com.pixelnx.localhelp.R;
import com.pixelnx.localhelp.model.ModelUserData;
import com.pixelnx.localhelp.spash.Database;
import com.pixelnx.localhelp.spash.Modellogo;
import com.pixelnx.localhelp.ui.login.ActivityLogin;
import com.pixelnx.localhelp.utils.Consts;
import com.pixelnx.localhelp.utils.ProjectUtils;
import com.pixelnx.localhelp.utils.sharedpref.SharedPref;
import com.pixelnx.localhelp.utils.widgets.CustomTextMedium;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Objects;

public class ActivityVandorDashBoard extends Activity implements View.OnClickListener {
RecyclerView rvList;
Context mContext;
    ArrayList<ModelItemsList> listData;
    ImageView profileIv;
    private SharedPref sharedPref;
    ImageView logo;
    Database dataBase;
    Modellogo modellogo;
    ModelUserData modelUserData;
    String count="0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vandor);
        mContext= ActivityVandorDashBoard.this;
        sharedPref = SharedPref.getInstance(mContext);

        modelUserData = sharedPref.getUser(Consts.Status.SP);



    }
    void countApi(){

        AndroidNetworking.post(Consts.Status.BASE_URL + Consts.Status.COUNT_NOTICE_API)
                .addBodyParameter("vendor_id",""+modelUserData.getData().getUserId())
                .setPriority(Priority.IMMEDIATE)
                .setTag(Consts.Status.SEARCH_API)
                .build()
                .getAsString(new StringRequestListener() {
                    @Override
                    public void onResponse(String response) {


                        try {
                            JSONObject jsonObject=new JSONObject(response);
                            if(jsonObject.getString("status").equalsIgnoreCase("1")){
                                count=jsonObject.getString("vendorNotice");

                                initial();
                            }



                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        ProjectUtils.pauseProgressDialog();
                    }

                    @Override
                    public void onError(ANError anError) {

                        ProjectUtils.pauseProgressDialog();

                    }
                });
    }
    void initial(){
        logo=findViewById(R.id.logo);
        dataBase = Room.databaseBuilder
                (getApplicationContext(),
                        Database.class, "database-name").fallbackToDestructiveMigration()
                .allowMainThreadQueries().build();
        modellogo=new Modellogo();
        modellogo= dataBase.daoAccess().getAllSetData();


        if(!modellogo.getLogoId().isEmpty()){
            Glide
                    .with(mContext)
                    .load(Consts.Status.BASE_IMAGE_URL +modellogo.getLogoImage())
                    .override(400, 200)
                    .fitCenter()
                    .into(logo);

        }
        profileIv=findViewById(R.id.profileIv);
        profileIv.setOnClickListener(this);
        rvList=findViewById(R.id.rvList);
        ProjectUtils.pauseProgressDialog();
        listData = new ArrayList<>();

        listData.add(0,new ModelItemsList("Notifications ("+count+")", R.drawable.notice));
        listData.add(1,new ModelItemsList("All Items", R.drawable.allitem));
        listData.add(2,new ModelItemsList("Add Item", R.drawable.add));
        listData.add(3,new ModelItemsList("Profile", R.drawable.user));
        listData.add(4,new ModelItemsList("New Orders", R.drawable.contract));
        listData.add(5,new ModelItemsList("Delivered Orders", R.drawable.deliverd));
        listData.add(6,new ModelItemsList("Cancelled Orders ", R.drawable.cancel));

        listData.add(new ModelItemsList("Logout", R.drawable.logout));
        rvList.setLayoutManager(new GridLayoutManager(mContext, 1));
        AdapterDashBoard adapterDashBoard = new AdapterDashBoard(mContext, listData);


        rvList.setAdapter(adapterDashBoard);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.profileIv) {
            logoutPopupDia();
        }

    }

    @Override
    protected void onResume() {
        if (ProjectUtils.checkConnection(mContext)) {
            countApi();
        } else {
            Toast.makeText(mContext, "No internet connection...Try again! ", Toast.LENGTH_SHORT).show();


        }

        super.onResume();
    }


    void logoutPopupDia() {
        final Dialog dialog ;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            dialog = new Dialog(mContext, android.R.style.Theme_Material_Light_Dialog_Alert);
        } else {
            dialog = new Dialog(mContext);
        }
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.logout_popups);
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        CustomTextMedium dialogYesButton = dialog.findViewById(R.id.btnYes);
        dialogYesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                sharedPref.clearAllPreferences();
                sharedPref.setBooleanValue(Consts.Status.IS_REGIS,false);
                Intent loginScreen = new Intent(mContext, ActivityLogin.class);
                loginScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                mContext.startActivity(loginScreen);

            }
        });


        CustomTextMedium dialogNoButton = dialog.findViewById(R.id.btnNo);
        dialogNoButton.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {

                dialog.dismiss();
            }
        });

        dialog.show();
    }
    private void exitAppDialog() {
        final     AlertDialog.Builder builder ;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder = new AlertDialog.Builder(mContext, android.R.style.Theme_Material_Light_Dialog_Alert);
        } else {
            builder = new AlertDialog.Builder(mContext);
        }

        builder.setMessage("Are you sure you want to close this app?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
    @Override
    public void onBackPressed() {
        exitAppDialog();

    }
}
