package com.example.applock.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.work.WorkManager;

import com.example.applock.R;
import com.example.applock.allmanager.AppManager;
import com.example.applock.allmanager.SoftLockPermission;
import com.example.applock.allmanager.SoftWarePerssionDialog;
import com.example.applock.lockadapter.ViewPaperAdapter;
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 java.util.ArrayList;
import java.util.List;
import com.example.applock.fragment.SoftWareLockFragment;

public class MainActivity extends AppCompatActivity implements PermissionBtnListener{
    private TextView textview_softlock;
    private TextView reset_key;
    private TextView version;
    private TextView text_version;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private String[] tabTitle;
    private ImageView image_setting;
    private WorkManager workManager;
    private RelativeLayout reset_key_lay;
    private DrawerLayout main_draw;
    private SoftWarePerssionDialog dialog;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text_version = findViewById(R.id.text_version);
        workManager = WorkManager.getInstance(this);
        viewPager = findViewById(R.id.view_page_main);
        main_draw = findViewById(R.id.main_draw);
        tabLayout = findViewById(R.id.app_table);
        image_setting = findViewById(R.id.image_set);
        reset_key_lay = findViewById(R.id.reset_key_lay);
        image_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                main_draw.openDrawer(Gravity.RIGHT);
            }
        });
        startService(new Intent(this,MySoftWareLockService.class));
        initTableLayoutViewPaper();

        getPermission();
        reset_key_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoReset();
            }
        });

    String appVersions = AppManager.getAppVersions(this);
        if (appVersions == null) {
            text_version.setText("V1.0.0");
        } else {
            String format = String.format(getString(R.string.app_lock_version), appVersions);
            text_version.setText(format);
        }
    }

    private void gotoReset() {
        Intent intent = new Intent(MainActivity.this, KeyWordActivity.class);
        intent.putExtra(MyAllValues.PASS_KEY_WORD, 2);
        startActivity(intent);
    }

    private void initTableLayoutViewPaper() {
        tabTitle = new String[]{getString(R.string.unlocked), getString(R.string.locked)};
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(SoftWareLockFragment.newInstancePage(false));
        fragmentList.add(SoftWareLockFragment.newInstancePage(true));
        ViewPaperAdapter viewPaperAdapter= new ViewPaperAdapter(getSupportFragmentManager(), fragmentList, tabTitle);
        viewPager.setAdapter(viewPaperAdapter);
        Log.d("-------","----tabtitle"+tabTitle);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                SoftWareLockFragment fragment = (SoftWareLockFragment) fragmentList.get(position);
                fragment.refresh();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void getPermission() {
        boolean getUsagePermission = SoftLockPermission.isGetUsagePermission(this);
        boolean overlays = SoftLockPermission.isOverlays(this);
        if (getUsagePermission && overlays) {

        } 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();
            }
        }
    }
}