package com.cygnusa.hyperm;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import com.google.android.material.tabs.TabLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.cygnusa.hyperm.adapter.ViewPagerAdapter;
import com.cygnusa.hyperm.util.GlobalData;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int PERMISSION_REQUEST_CODE = 1;

    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;
    TabLayout tabLayout;

    public static ImageView img_broucher, img_rating;
    public static ImageView layout1;
    private Handler.Callback callback;
   // public int tabTitles[] = new int[]{R.string.exterior_title, R.string.interior_title};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_explore);
        initViews();

        if (GlobalData.user.getData().getRole().equalsIgnoreCase("16")) {
            layout1.setVisibility(View.GONE);
            img_rating.setVisibility(View.VISIBLE);
            img_broucher.setVisibility(View.VISIBLE);
        } else {
            layout1.setVisibility(View.VISIBLE);
            img_rating.setVisibility(View.GONE);
            img_broucher.setVisibility(View.GONE);
        }

        if (checkPermissionStorage()) {
            requestPermission();
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (GlobalData.user.getData().getRole().equalsIgnoreCase("16")) {
            startActivity(new Intent(MainActivity.this, ProductWelcomeScreenActivity.class));
        }
    }

    private boolean checkPermissionStorage() {
        int result = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.e("value", "Permission Granted, Now you can use local drive .");
                    viewPagerAdapter = new ViewPagerAdapter(MainActivity.this,getSupportFragmentManager());
                    viewPager.setAdapter(viewPagerAdapter);
                } else {
                    Log.e("value", "Permission Denied, You cannot use local drive .");
                }
                break;
        }
    }

    private void requestPermission() {

        if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
        }
    }

    private void initViews() {
        //initHomeFragment(new Hotspot360());
        viewPager = findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(MainActivity.this,getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);

        tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        img_broucher = findViewById(R.id.imgBroucher);
        img_broucher.setOnClickListener(this);

        img_rating = findViewById(R.id.img_rating);
        img_rating.setOnClickListener(this);

        layout1 = findViewById(R.id.fabEnquiry);
        layout1.setOnClickListener(this);
        callback = new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (GlobalData.canLogout) {
                    startActivity(new Intent(getBaseContext(), LoginActivity.class));
                    finish();
                }
                return false;
            }
        };
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgBroucher:
                startActivity(new Intent(this, HyperMEnquiryForm.class));
                break;
            case R.id.img_rating:
                startActivity(new Intent(this, ProductFeedbackFormActivity.class));
                break;
            default:
        }
    }
}
