package tw.com.altob.goodpk.Fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import tw.com.altob.goodpk.R;
import tw.com.altob.goodpk.Util.PermissionCode;
import tw.com.altob.goodpk.Util.PermissionUtil;

public class HelpFragment extends Fragment {

    private Button callOutBtn;
    private String phoneNum = "0229647078";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.activity_help, container, false);

        this.callOutBtn = (Button) contentView.findViewById(R.id.callOutBtn);

        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.callOutBtn.setOnClickListener( this.onCallOutBtnClickListener);
    }

    private Button.OnClickListener onCallOutBtnClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            int title = R.string.action_import;
            int content = R.string.action_import_content;
            showAlert( title, content);
        }
    };

    //權限回調
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == PermissionCode.PERMISSION_PHONE) {
            if (PermissionUtil.checkPhonePermission(this.getActivity())) {
                callPhone(phoneNum);
            } else{
                Toast.makeText( getContext(),getString(R.string.about_callphone_error), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void callPhone(String phoneNum) {

        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    private void showAlert(int titleID, int contentID){
        AlertDialog.Builder dialog = new AlertDialog.Builder( getActivity());
        dialog.setTitle( getString( titleID));
        dialog.setMessage( getString( contentID));

        dialog.setNegativeButton( "確認",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                PermissionUtil.requestPhonePermissions(HelpFragment.this, PermissionCode.PERMISSION_PHONE);
            }

        });


        dialog.show();
    }
}
