package com.must7hsan.androidutils.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.must7hsan.androidutils.fileutils.FileUtils;
import com.must7hsan.androidutils.image.ImagePicker;
import com.must7hsan.androidutils.permission.PermissionGroup;
import com.must7hsan.androidutils.permission.PermissionRequest;

/**
 * Created by Mustahsan on 03-Aug-17.
 */

public class BaseAppCompatActivity extends AppCompatActivity {
    protected static PermissionRequest storagePermission;
    protected static PermissionRequest cameraAndStoragePermission;
    protected static PermissionRequest cameraPermission;
    protected static PermissionRequest contactPermission;
    protected static PermissionRequest locationPermission;

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        initViews();
        setViewsValue();
        setListeners();
        preparePermissions();
        askPermissions();
    }

    protected void setToolbar(Toolbar toolbar, boolean allowBack) {
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null && allowBack) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void preparePermissions() {
        storagePermission = new PermissionRequest(this, PermissionGroup.STORAGE) {
            @Override
            protected void completelyGranted() {
                super.completelyGranted();
                FileUtils.initializeAppDir();
            }
        };
        cameraAndStoragePermission = new PermissionRequest(this, PermissionGroup.CAMERA_AND_STORAGE) {
            @Override
            protected void completelyGranted() {
                super.completelyGranted();
                FileUtils.initializeAppDir();
            }
        };
        cameraPermission = new PermissionRequest(this, PermissionGroup.CAMERA_ONLY);
        contactPermission = new PermissionRequest(this, PermissionGroup.CONTACTS);
        locationPermission = new PermissionRequest(this, PermissionGroup.LOCATION);
    }

    protected void initViews() {
    }

    protected void setViewsValue() {
    }

    protected void setListeners() {
    }

    protected void askPermissions() {

    }

    protected void startPickImageActivity(int requestCode) {
        startActivityForResult(ImagePicker.getPickImageIntent(this), requestCode);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (storagePermission != null) {
            storagePermission.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        if (cameraAndStoragePermission != null) {
            cameraAndStoragePermission.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        if (contactPermission != null) {
            contactPermission.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        if (cameraPermission != null) {
            cameraPermission.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        if (locationPermission != null) {
            locationPermission.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
