package tw.com.altob.goodpk.Fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.loader.content.Loader;

import com.ga.sdk.api.ApiResult;

import tw.com.altob.goodpk.Bean.CS;
import tw.com.altob.goodpk.Loader.ApiLoaderCallback;
import tw.com.altob.goodpk.Loader.ChengePWLoader;
import tw.com.altob.goodpk.Loader.GetCarSecurityLoader;
import tw.com.altob.goodpk.R;
import tw.com.altob.goodpk.Util.CommonIntent;
import tw.com.altob.goodpk.Util.PrefsUtil;

public class PWFragment extends Fragment {

    private Button DoneBtn;
    private EditText AcctText,PwText, nPwText;

    private String mAcct, mPwd, mNPwd , mkey;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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

        this.DoneBtn = (Button) contentView.findViewById(R.id.DoneBtn);
        this.AcctText = (EditText) contentView.findViewById(R.id.AcctText);
        this.PwText = (EditText) contentView.findViewById(R.id.PwText);
        this.nPwText = (EditText) contentView.findViewById(R.id.nPwText);

        return contentView;
    }

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

        this.DoneBtn.setOnClickListener( this.onLoginBtnClickListener);
    }

    private Button.OnClickListener onLoginBtnClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(AcctText.getText().length() >0 && PwText.getText().length() >0 && nPwText.getText().length() > 0){
                //API
                mAcct = AcctText.getText().toString();
                mPwd = PwText.getText().toString();
                mNPwd = nPwText.getText().toString();
                //Log.d("test", "onClick: " + (mAcct + "i" + mPwd + "iii" + mNPwd));
                mkey = PrefsUtil.md5((mAcct + "i" + mPwd + "iii" + mNPwd));

                ApiLoaderCallback.startApiLoader(getActivity(), getChengePWLoaderCallback);
            }else{
                int title = R.string.action_error;
                int content = R.string.action_error_content2;
                showAlert( title, content);
            }
        }
    };

    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
            }

        });


        dialog.show();
    }

    //查詢鎖車狀態
    private ApiLoaderCallback<CS> getChengePWLoaderCallback = new ApiLoaderCallback<CS>() {

        @Override
        public Loader<ApiResult<CS>> onCreateLoader(int id, Bundle args) {

            return new ChengePWLoader( getActivity(), mAcct, mPwd, mNPwd, mkey);
        }

        @Override
        protected void onStatusOk(CS data) {
            //Log.d("test", "onStatusOk: " + data.getResult_code());
            //Log.d("test", "onStatusOk: " + data.getResult_msg());

            if(data.getResult_code().contains("OK")){
                Toast.makeText( getContext(), getString(R.string.action_change), Toast.LENGTH_SHORT).show();
                getActivity().finish();
            }else{
                Toast.makeText( getContext(), getString(R.string.action_error_lock), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected void onPreLoadFinished() {

        }
    };
}
