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.GetCarSecurityLoader;
import tw.com.altob.goodpk.R;
import tw.com.altob.goodpk.Util.CommonIntent;
import tw.com.altob.goodpk.Util.ExtraKey;
import tw.com.altob.goodpk.Util.PrefsUtil;

public class CarSecurityFragment extends Fragment {

    private Button LoginBtn,ChangeBtn;
    private EditText AcctText,PwText;

    private String mAcct, mPwd, mStatus , mkey, station;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        station =  this.getArguments().getString(ExtraKey.DATA);
    }

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

        this.LoginBtn = (Button) contentView.findViewById(R.id.LoginBtn);
        //this.ChangeBtn = (Button) contentView.findViewById(R.id.ChangeBtn);
        this.AcctText = (EditText) contentView.findViewById(R.id.AcctText);
        this.PwText = (EditText) contentView.findViewById(R.id.PwText);

        return contentView;
    }

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

        this.LoginBtn.setOnClickListener( this.onLoginBtnClickListener);
        //this.ChangeBtn.setOnClickListener( this.onChangeBtnClickListener);
    }

    private Button.OnClickListener onLoginBtnClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(AcctText.getText().length() >0 && PwText.getText().length() >0){
                //API
                mAcct = AcctText.getText().toString();
                mPwd = PrefsUtil.md5(PwText.getText().toString());
                mStatus = "2";
                //Log.d("test", "onClick: " + (mAcct + "i" + mPwd + "iii" + mStatus));
                mkey = PrefsUtil.md5((mAcct + "i" + mPwd + "iii" + mStatus));
                ApiLoaderCallback.startApiLoader(getActivity(), getCarSecurityLoaderCallback);
            }else{
                int title = R.string.action_error;
                int content = R.string.action_error_content;
                showAlert( title, content);
            }
        }
    };

    private Button.OnClickListener onChangeBtnClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            //跳修改密碼頁面
            Intent intent = new Intent();
            intent = CommonIntent.createChangePWActivity(getActivity());
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.right_in,0);
        }
    };

    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> getCarSecurityLoaderCallback = new ApiLoaderCallback<CS>() {

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

            return new GetCarSecurityLoader( getActivity(), mAcct, mPwd, mStatus, mkey, station);
        }

        @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("-99")){
                int title = R.string.action_ca_error;
                int content = R.string.action_ca_error_lock;
                showAlert( title, content);
            }

            if(data.getResult_code().contains("FAIL") || data.getResult_code().contains("fail")){
                int title = R.string.action_error;
                int content = R.string.action_error_car_security;
                showAlert( title, content);
            }else{
                Toast.makeText( getContext(), getString(R.string.action_login), Toast.LENGTH_SHORT).show();
                //登入成功，轉至控制畫面
                Intent intent = new Intent();
                intent = CommonIntent.createCarLockActivity(getActivity(), mAcct, mPwd, data.getResult_msg(), station);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.right_in,0);
            }
        }

        @Override
        protected void onPreLoadFinished() {

        }
    };
}
