package com.pixelnx.localhelp.spash;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;

import android.widget.ImageView;
import android.widget.Toast;



import androidx.room.Room;

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.ui.login.ActivityLogin;

import com.pixelnx.localhelp.ui.vendor.ActivityAllStore;
import com.pixelnx.localhelp.ui.vendor.ActivityVandorDashBoard;
import com.pixelnx.localhelp.utils.Consts;
import com.pixelnx.localhelp.utils.sharedpref.SharedPref;


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


public class ActivitySplash extends Activity {

    SharedPref sharedPref;
    ModelUserData modelUserData;
    Modellogo modelLogo;
    Context mContext;
    ImageView imageViewLogo;
    Database database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);
        mContext = ActivitySplash.this;

        

        sharedPref = SharedPref.getInstance(mContext);
        modelUserData = sharedPref.getUser(Consts.Status.SP);

        imageViewLogo=(ImageView) findViewById(R.id.logo);






        database = Room.databaseBuilder
                (getApplicationContext(),
                        Database.class, "database-name").fallbackToDestructiveMigration()
                .allowMainThreadQueries().build();
        modelLogo=new Modellogo();
       modelLogo= database.daoAccess().getAllSetData();
       if(modelLogo != null){
           if(modelLogo.getLogoId().isEmpty()){
apiLogo();
           }else{

               Glide
                       .with(mContext)
                       .load(Consts.Status.BASE_IMAGE_URL +modelLogo.getLogoImage())
                       .override(400, 200)
                       .fitCenter()
                       .into(imageViewLogo);
           }

       }else{
apiLogo();
       }
        new Handler().postDelayed(new Runnable() {


            @Override
            public void run() {


                checkStatus();


                finish();
            }
        },3000);
    }


    void apiLogo(){
        AndroidNetworking.post(Consts.Status.BASE_URL + Consts.Status.GET_LOGO_TITLE)
                .setPriority(Priority.IMMEDIATE)
                .build()
                .getAsString(new StringRequestListener() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject=new JSONObject(response);
                            if (jsonObject.getString("status").equalsIgnoreCase("1")) {

                                JSONArray jsonArray=jsonObject.getJSONArray("data");

                                JSONObject json=jsonArray.getJSONObject(0);
                                modelLogo=new Modellogo();
                                modelLogo.setLogoId(""+json.getString("logoId"));
                                modelLogo.setLogoImage(""+json.getString("logoImage"));
                                modelLogo.setLogoTitle(""+json.getString("logoTitle"));



                                database.daoAccess().insertTask(modelLogo);
                                Glide
                                        .with(mContext)
                                        .load(Consts.Status.BASE_IMAGE_URL +json.getString("logoImage"))
                                        .override(400, 200)
                                        .fitCenter()
                                        .into(imageViewLogo);

                            }

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




                    }

                    @Override
                    public void onError(ANError anError) {
                        Toast.makeText(mContext,"Something went wrong !",Toast.LENGTH_SHORT).show();

                    }
                });


    }

    void checkStatus() {

        if (sharedPref.getBooleanValue(Consts.Status.IS_REGIS)) {
            modelUserData=new ModelUserData();
            modelUserData = sharedPref.getUser(Consts.Status.SP);
            if(modelUserData.getData().getUserType() != null){
            if(modelUserData.getData().getUserType().equalsIgnoreCase("3")){
                Intent intent=new Intent(mContext, ActivityVandorDashBoard.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }else{
                startActivity(new Intent(mContext,  ActivityAllStore.class));
                finish();}

        } else {
            Intent intent = new Intent(mContext, ActivityLogin.class);
            startActivity(intent);
            finish();
        }}else{
            Intent intent = new Intent(mContext, ActivityLogin.class);
            startActivity(intent);
            finish();
        }

    }


}

