package com.example.applock.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;
import androidx.work.WorkManager;

import com.example.applock.R;
import com.example.applock.allmanager.LockAction;
import com.example.applock.allmanager.SoftLockPermission;
import com.example.applock.allmanager.SoftWarePerssionDialog;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.lockadapter.FragmentAdapter;
import com.example.applock.roombase.SoftWareLockRoom;
import com.example.applock.value.MyAllValues;
import com.google.android.material.tabs.TabLayout;
import com.example.applock.allmanager.MySoftWareLockService;
import com.example.applock.listener.PermissionBtnListener;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity implements PermissionBtnListener{
    private EditText search_text;
    private ImageView start_search;
    private String search_name;
    private ViewPager2 view_page_main;
    private TabLayout main_tablelayout;
    private String[] tabTitle;
    private ImageView image_setting;
    private WorkManager workManager;
    private SoftWarePerssionDialog dialog;
    private boolean gotoit = false;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        workManager = WorkManager.getInstance(this);
        view_page_main = findViewById(R.id.view_page_main);
        main_tablelayout = findViewById(R.id.main_tablelayout);
        image_setting = findViewById(R.id.image_set);
        search_text = findViewById(R.id.main_search_text);
        start_search = findViewById(R.id.main_start_search);
        image_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SettingActivity.class);
                startActivity(intent);
            }
        });
        start_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SoftLockPermission.isGetUsagePermission(MainActivity.this) && SoftLockPermission.isOverlays(MainActivity.this)){
                    search_name = search_text.getText().toString();
                    Intent intent =new Intent(MainActivity.this,SearchActivity.class);
                    intent.putExtra(MyAllValues.search_name,search_name);
                    startActivity(intent);
                }else {
                    getPermission();
                }
            }
        });
        startService(new Intent(this,MySoftWareLockService.class));
        setViewPaper();
        getPermission();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(SoftWareLockApplication.TAG,"======onResume");
    }

    private void setViewPaper() {
        int tabIconColor = ContextCompat.getColor(MainActivity.this, R.color.table_unselect);
        view_page_main.setAdapter(new FragmentAdapter(this));
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(main_tablelayout, view_page_main, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position) {
                    case 0:
                        tab.setIcon(R.drawable.system_src);
                        tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                        tab.setText(R.string.system_text);
                        break;
                    case 1:
                        tab.setIcon(R.drawable.third_src);
                        tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                        tab.setText(R.string.third_text);
                        break;
                    case 2:
                        tab.setIcon(R.drawable.locked_src);
                        tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                        tab.setText(R.string.locked);
                        break;
                }
            }
        });
        tabLayoutMediator.attach();
    }

    public void getPermission() {
        boolean getUsagePermission = SoftLockPermission.isGetUsagePermission(this);
        boolean overlays = SoftLockPermission.isOverlays(this);
        if (getUsagePermission && overlays) {
           // hideDialog();
        } else {
            if (dialog == null) {
                dialog = new SoftWarePerssionDialog(this);
                dialog.setPermissionBtnListener(this);
            }
            dialog.show(getSupportFragmentManager(), "");
        }
    }


    @Override
    public void onClickUsage() {
        SoftLockPermission.requestUsage(this, MyAllValues.request_code_usage);
    }

    @Override
    public void onClickOverlay() {
        SoftLockPermission.requestOverlays(this, MyAllValues.request_code_overlay);
    }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
           case MyAllValues.request_code_usage, MyAllValues.request_code_overlay:
               hideDialog();
               break;
        }
    }
    private void hideDialog() {
        if (SoftLockPermission.allAllowPermission(this)) {
            if (dialog != null) {
                dialog.dismiss();
            }
        }
    }
}