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.ImageView;
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 CarLockFragment extends Fragment {

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

    private Button lockBtn, unlockBtn, ChangeBtn;
    private ImageView carImg, carImg2;

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

        this.mAcct = this.getArguments().getString(ExtraKey.ID);
        this.mPwd = this.getArguments().getString(ExtraKey.DATA);
        this.msg = this.getArguments().getString(ExtraKey.OPTION);
        this.station = this.getArguments().getString(ExtraKey.LOCATION);
    }

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

        this.lockBtn = (Button) contentView.findViewById(R.id.lockBtn);
        this.unlockBtn = (Button) contentView.findViewById(R.id.unlockBtn);
        //this.ChangeBtn = (Button) contentView.findViewById(R.id.ChangeBtn);
        this.carImg = (ImageView) contentView.findViewById(R.id.carImg);
        this.carImg2 = (ImageView) contentView.findViewById(R.id.carImg2);

        return contentView;
    }

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

        if(msg.contains("unlocked")){
            carImg2.setVisibility(View.INVISIBLE);
            carImg.setVisibility(View.VISIBLE);
            unlockBtn.setEnabled(false);
            lockBtn.setEnabled(true);
        }else{
            carImg2.setVisibility(View.VISIBLE);
            carImg.setVisibility(View.INVISIBLE);
            unlockBtn.setEnabled(true);
            lockBtn.setEnabled(false);
        }

        this.lockBtn.setOnClickListener( this.onLockBtnClickListener);
        this.unlockBtn.setOnClickListener( this.onUnlockBtnClickListener);
        //this.ChangeBtn.setOnClickListener( this.onChangeBtnClickListener);
    }

    //鎖車
    private Button.OnClickListener onLockBtnClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            mStatus = "1";
            mkey = PrefsUtil.md5((mAcct + "i" + mPwd + "iii" + mStatus));
            ApiLoaderCallback.startApiLoader(getActivity(), setLockLoaderCallback);
        }
    };

    //解鎖
    private Button.OnClickListener onUnlockBtnClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            mStatus = "0";
            mkey = PrefsUtil.md5((mAcct + "i" + mPwd + "iii" + mStatus));
            ApiLoaderCallback.startApiLoader(getActivity(), setUnLockLoaderCallback);
        }
    };

    //改密碼
    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> setLockLoaderCallback = 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("OK")){
                Toast.makeText( getContext(), getString(R.string.action_lock), Toast.LENGTH_SHORT).show();
                carImg2.setVisibility(View.VISIBLE);
                carImg.setVisibility(View.INVISIBLE);
                unlockBtn.setEnabled(true);
                lockBtn.setEnabled(false);
            }else{
                if(data.getResult_code().contains("-99")){
                    int title = R.string.action_ca_error;
                    int content = R.string.action_ca_error_lock;
                    showAlert( title, content);
                }else{
                    int title = R.string.action_error;
                    int content = R.string.action_error_lock;
                    showAlert( title, content);
                }

            }
        }

        @Override
        protected void onPreLoadFinished() {

        }
    };

    //解鎖
    private ApiLoaderCallback<CS> setUnLockLoaderCallback = 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("OK")){
                Toast.makeText( getContext(), getString(R.string.action_unlock), Toast.LENGTH_SHORT).show();
                carImg2.setVisibility(View.INVISIBLE);
                carImg.setVisibility(View.VISIBLE);
                unlockBtn.setEnabled(false);
                lockBtn.setEnabled(true);
            }else{
                if(data.getResult_code().contains("-99")){
                    int title = R.string.action_ca_error;
                    int content = R.string.action_ca_error_lock;
                    showAlert( title, content);
                }else{
                    int title = R.string.action_error;
                    int content = R.string.action_error_lock;
                    showAlert( title, content);
                }
            }
        }

        @Override
        protected void onPreLoadFinished() {

        }
    };
}
