package com.renitha.Activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.renitha.Interface.APIService;
import com.renitha.Model.APIUrl;
import com.renitha.Model.SetterProfile;
import com.renitha.Pref.Pref;
import com.renitha.R;

import me.ibrahimsn.lib.OnItemSelectedListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ProfileActivity extends AppCompatActivity
{
    TextView name,desig,userName,userConatctNo,userEmail,userAddress,user_name,userPassword;
    private me.ibrahimsn.lib.SmoothBottomBar bottomNavigationView;
    private ImageView toolbar_contact;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_profile);
        name            =   (TextView)findViewById(R.id.name);
        desig           =   (TextView)findViewById(R.id.desig);
        userName        =   (TextView)findViewById(R.id.userName);
        userConatctNo   =   (TextView)findViewById(R.id.userConatctNo);
        userEmail       =   (TextView)findViewById(R.id.userEmail);
        userAddress     =   (TextView)findViewById(R.id.userAddress);
        user_name       =   (TextView)findViewById(R.id.user_name);
        userPassword    =   (TextView)findViewById(R.id.userPassword);
        toolbar_contact     =   (ImageView)findViewById(R.id.toolbar_contact);
        toolbar_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent   =   new Intent(ProfileActivity.this, ContactUsActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.enter, R.anim.exit);
            }
        });
        bottomNavigationView = (me.ibrahimsn.lib.SmoothBottomBar) findViewById(R.id.navigation);
        bottomNavigationView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public boolean onItemSelect(int i)
            {
                // Toast.makeText(getApplicationContext(),"selected"+i,Toast.LENGTH_LONG).show();
                if(i==0)
                {
                    if( Pref.getInstance(getApplicationContext()).getLoginType().equals("Customer")) {
                        Intent intent1 = new Intent(ProfileActivity.this, ViewCustomerComplaints.class);
                        startActivity(intent1);
                        overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
                    }
                    else if( Pref.getInstance(getApplicationContext()).getLoginType().equals("User")) {
                        Intent intent1 = new Intent(ProfileActivity.this, ViewUserComplaints.class);
                        startActivity(intent1);
                        overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
                    }
                }
                else if (i==2)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(ProfileActivity.this);
                    builder.setMessage("Are you sure want to exit?")
                            .setCancelable(false)
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id)
                                {
                                    Intent intent = new Intent(ProfileActivity.this, LoginTypeActivity.class);
                                    Pref.getInstance(getApplicationContext()).setLoginId("");
                                    Pref.getInstance(getApplicationContext()).setLoginType("");
                                    Pref.getInstance(getApplicationContext()).setIsLogin(false);

                                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    startActivity(intent);
                                    dialog.dismiss();
                                }
                            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            bottomNavigationView.setActiveItem(0);
                            dialog.dismiss();

                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show();
                    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
                    nbutton.setTextColor(Color.BLACK);
                    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
                    pbutton.setTextColor(Color.BLACK);
                }
                return false;
            }
        });
        bottomNavigationView.setActiveItem(1);
        JSON_GET_DOC_PROFILE();
    }
    private void JSON_GET_DOC_PROFILE()
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Wait...");
        progressDialog.show();
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(APIUrl.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService service = retrofit.create(APIService.class);

        Call<SetterProfile> call = service.getMyProfile(Pref.getInstance(getApplicationContext()).getLoginId(),Pref.getInstance(getApplicationContext()).getLoginType());
        call.enqueue(new Callback<SetterProfile>()
        {
            @Override
            public void onResponse(Call<SetterProfile> call, Response<SetterProfile> response)
            {
                progressDialog.dismiss();
                if (response.body().getError().equals(true))
                {
                    name.setText(response.body().getUserName());
                    userName.setText(response.body().getUserName());
                    desig.setText(response.body().getDesig());
                    userConatctNo.setText(response.body().getUserConatctNo());
                    userEmail.setText(response.body().getUserEmail());
                    userAddress.setText(response.body().getUserAddress());
                    user_name.setText(response.body().getUser_name());
                    userPassword.setText(response.body().getUserPassword());


                    /*Glide.with(getContext())
                            .load(response.body().getPhoto_img())
                            .apply(new RequestOptions().centerCrop()
                                    .circleCrop())
                            .into(imgViewPic);
                    */
                }
            }
            @Override
            public void onFailure(Call<SetterProfile> call, Throwable t)
            {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }
    @Override
    public void onBackPressed() {

    }

}
